I have a site with the following htaccess file:
RewriteCond %{THE_REQUEST} \s/+(.+?)\index.html/?[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?uri=$1 [NC,L,QSA]
There are two pages that I need to redirect to a different domain. Currently I have them setup as 302 redirects with the following line:
Redirect 302 /some-url-slug https://example.com/some-new-url-slug
This does work to redirect to the new domain but the issue is it's adding the uri parameter to the redirected url so I get something like:
https://example.com/some-new-url-slug?uri=some-url-slug
I thought adding it above the rewrite rule would solve it, but it does not. How can I omit that rule but only for 2 specific pages?
The
Redirect
directive belongs to mod_alias, the other directives are mod_rewrite. It doesn't matter where you place theRedirect
directive, it will always be processed after mod_rewrite. And theRedirect
directive will preserve the query string.You need to use a mod_rewrite
RewriteRule
instead, at the top of the file, for these two specific redirects. For example:Note that the URL-path matched by the
RewriteRule
directive does not start with a slash. TheQSD
(Query String Discard) flag ensures that any query string that might be present on the initial request is discarded (otherwise it is passed through by default).UPDATE:
You can include this in the same rule and make the trailing slash optional with the addition of
/?
at the end of the pattern. For example:This now matches both
some-url-slug
orsome-url-slug/
and redirects to the URL as before.