RewriteCond %{QUERY_STRING} ^newsId=(.*)$ [NC]
RewriteRule ^$ https://www.new.example/actualites/%1? [NC,L,R]
There are a couple of problems with this:
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).
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: