Reputation: 517
I would like to create a mod_rewrite rule that redirects this:
www.domain.com/original/
To this with an appended anchor:
www.domain.com/new/#something-else
It should also preserve any query string specified in the original URL, when present. For example:
www.domain.com/original/?key=example
Should redirect to:
www.domain.com/new/?key=example#something-else
I tried using the following mod_rewrite rule with the NE and QSA flags, and even though it preserves the anchor and query string, they are rewritten in the wrong order: query string after anchor instead of before.
RewriteRule ^original https://%{HTTP_HOST}/new/$1#something-else [QSA,NE,R=301,L]
How can I preserve the original query string (when present) and append the anchor to the end?
Thanks
Upvotes: 1
Views: 248
Reputation: 785721
How can I preserve the original query string (when present) and append the anchor to the end?
Flag QSA
is not needed unless you are modifying query string in your redirect, hence that can be omitted here.
There is no need to use https://%{HTTP_HOST}
in your rule because source and redirected domains are same.
$1
will be empty because you're not capturing any text in your pattern.
You may use this redirect rule:
# when query string is non-empty
RewriteCond %{QUERY_STRING} .
RewriteRule ^original/?$ /new/?%{QUERY_STRING}#something-else [NC,NE,R=302,L]
# when query string is empty
RewriteRule ^original/?$ /new/#something-else [NC,NE,R=302,L]
Once you verify it is working fine, replace R=302
to R=301
(permanent redirect)
Upvotes: 1