I'm using the following directives in .htaccess to redirect only example.com/script-dir/index.php (with www and without) to example.com/index.php. But it also redirects example.com/script-dir/index.php?page=faq and other pages.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^script\-dir\/index\.php$ "http\:\/\/example\.com\/" [R=301,L]

What should I do to redirect only that single page?


The query string (?page=faq) isn't part of the URL that gets tested in the RewriteRule, so you have to add a separate condition to test it:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^script-dir/index\.php$ http://example.com/ [R=301,L]

I'm not sure why other pages would get redirected though.


You can RewriteCond based on empty QUERY_STRING, following same approach as in this other answer: https://stackoverflow.com/a/7591367/4363163 . Also, you might prefer exact match, without relying on RegExp, as explained in this other answer: https://stackoverflow.com/a/24476841/4363163

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.