1

I want to do an url redirect to a new domain by retrieving the ID parameter but only taking the first 4 characters. Anyone know how to do this?

For example, an original url:

http://www.original.example/see/news/actualite.php?newsId=be9e836&newsTitle="blablabla"

To :

https://www.new.example/actualites/be9e

I have tested :

RewriteCond %{QUERY_STRING} ^newsId=(.*)$ [NC]
RewriteRule ^$ https://www.new.example/actualites/%1? [NC,L,R]
MrWhite
  • 43,179
  • 8
  • 60
  • 84
remi3889
  • 13
  • 3

1 Answers1

0
RewriteCond %{QUERY_STRING} ^newsId=(.*)$ [NC]
RewriteRule ^$ https://www.new.example/actualites/%1? [NC,L,R]

There are a couple of problems with this:

  1. The regex ^$ in the RewriteRule pattern only matches the document root. The URL in your example is /see/news/actualite.php - so this rule will never match (and the conditions are never processed).

  2. The regex ^newsId=(.*)$ is capturing everything after newsId=, including any additional URL parameters. You only need the first 4 characters of this particular URL param.

As an aside, your existing condition is dependent on newsId being the first URL parameter. Maybe this is always the case, maybe not. But it is relatively trivial to check for this URL parameter, regardless of order.

Also, do you need a case-insensitive match? Or is it always newsId as stated in your example. Only use the NC flag if this is necessary, not as a default.

Try the following instead:

RewriteCond %{QUERY_STRING} (?:^|&)newsId=([^&]{4})
RewriteRule ^see/news/actualite\.php$ https://www.new.example/actualites/%1 [QSD,R,L]

The %1 backreference now contains just the first 4 characters of the newsId URL parameter value (ie. non & characters), as denoted by the regex ([^&]{4}).

The QSD flag (Apache 2.4) discards the original query string from teh redirect response. No need to append the substitution string with ? (an empty query string), as would have been required in earlier versions of Apache.


UPDATE:

I have an anchor link (#) which is added at the end of the link, is there a possibility of deleting it to make a clean link? Example, currently I have: https://www.new.example/news/4565/#title Ideally : https://www.new.example/news/4565

The "problem" here is that the browser manages the "fragment identifier" (fragid) (ie. the "anchor link (#)") and preserves this through the redirect. In other words, the browser re-appends the fragid to the redirect response from the server. The fragid is never sent to the server, so we cannot detect this server side prior to issuing the HTTP redirect.

The only thing we can do is to append an empty fragid (ie. a trailing #) in the hope that the browser discards the original fragment. Unfortunately, you will likely end up with a trailing # on your redirected URLs (browser dependent).

For example (simplified):

:
RewriteRule .... https://example.com/# [R=301,NE,L]

Note that you will need the NE flag here to prevent Apache from URL-encoding the # in the redirect response.

Like I say above, browsers might handle this differently.

Further reading:

MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • 1
    Hello, thank you very much this is fabulous this is exactly what I was looking to do. A big thank you to you. One last little detail, I have an anchor link (#) which is added at the end of the link, is there a possibility of deleting it to make a clean link? Example, currently I have: https://www.new.example/news/4565/#title Ideally : https://www.new.example/news/4565/ – remi3889 Aug 29 '22 at 14:32
  • @remi3889 The "anchor link (#)" is called the _fragment identifier_ (fragid). The problem with this is that it is managed entirely by the browser. It is not sent to the server and the browser re-appends the fragid to the redirect response. The only thing we can do is append an empty fragid (to all redirect responses) in the hope that the browser removes it. I've updated my answer. – MrWhite Aug 29 '22 at 18:12
  • Hello, thank you very much for your return, in itself it does not matter to have an anchor link which refers to nothing. I will leave it like this. Thanks a lot :) – remi3889 Aug 30 '22 at 10:45