I am using a template on my website. This template is designed in a way where everything is in the index.php
file but the functions are executed based on the query string which is stick to the index.php
file.
I want to permanently redirect to the root mysite.com
when my site is accessed with index.php
with no query string parameters, but if there is any query string parameter then it should not be redirected and should remain the same.
To achieve this, this is what I added in my .htaccess
file, but it affects the query string and the site is unable to be navigated properly.
... RewriteCond %{THE_REQUEST} ^.*/index.php RewriteRule ^(.*)index.php$ /$1 [R=301,L] ...
Thanks for the help.
Advertisement
Answer
RewriteCond %{THE_REQUEST} ^.*/index.php RewriteRule ^(.*)index.php$ /$1 [R=301,L]
The regex ^.*/index.php
matches index.php
followed by anything, which naturally includes the query string.
If it’s always index.php
in the document root then the above can be further simplified, since there is never going to be anything before /index.php
in the URL-path.
Try the following instead:
RewriteCond %{THE_REQUEST} s/index.phps RewriteRule ^index.php$ / [R=301,L]
Note the space (ie. s
) after index.php
in the preceding CondPattern
to signify the end of the URL.
Test first with a 302 (temporary) redirect to avoid potential caching issues. You will need to clear your browser cache before testing since the erroneous 301 will have been cached.