einaregilsson / Redirector

Browser extension (Firefox, Chrome, Opera, Edge) to redirect urls based on regex patterns, like a client side mod_rewrite.
http://einaregilsson.com/redirector/
MIT License
1.55k stars 165 forks source link

[fr] Subtraction of Characters from Result #307

Open ADHDefy opened 2 years ago

ADHDefy commented 2 years ago

I'm gonna try my best to describe this, and I sure hope it makes sense. lol Regex is not my area of expertise.

In my example, I want to create a redirect from Hacker News (https://news.ycombinator.com/) to hckrnws (https://www.hckrnws.com/), an alternative frontend.

Here's the trouble: the format of the two URLs can be pretty different. For instance, here is the URL for the comments of an article on Hacker News:

https://news.ycombinator.com/item?id=31438989

Here is the same comment section for the same article on hckrnws:

https://www.hckrnws.com/stories/31438989

I can easily write an include pattern like https://news.ycombinator.com/.*=(\d+), and a redirect to https://www.hckrnws.com/stories/$1 and it will work.

BUT my redirect will not work if I simply want to redirect https://news.ycombinator.com/ to https://www.hckrnws.com/, because it will actually redirect to https://www.hckrnws.com/stories/, which is invalid.

Here's my proposal: If there was some sort of character that could indicate a removal of previous characters, I could make this work. For instance, I could write something like:

Example URL: https://news.ycombinator.com/item?id=31438989 Include Pattern: https://news.ycombinator.com(/)(?!item)|https://news.ycombinator.com/.*(?<==)(\d+) Redirect to: https://www.hckrnws.com/stories/(-9)$1$2

The (-9) could be replaced by some kind of delimiter that would indicate that (-9)$1 means, "subtract nine characters, then concatenate the value of $1." This way, it would read in such a way that if the given URL simply ends in a "/" with no characters after, then it will replace /stories/ with simply /; but, if the given URL continues after the "/`," then it should simply tack on the value of $2.

So if the example URL was https://news.ycombinator.com/item?id=31438989, the example result should read https://www.hckrnws.com/stories/31438989. If, however, I replaced my example URL with https://news.ycombinator.com/, then the example result should simply read, https://www.hckrnws.com/.

Again, I really hope this makes sense. Please let me know if I can clarify anything here.

Gitoffthelawn commented 2 years ago

Hi @ADHDefy!

I like the goal of this idea, as I've encountered similar situations.

Another way to solve this issue is to enable recursive redirects. See https://github.com/einaregilsson/Redirector/issues/152