John Davidson

apache - .htaccess - Permanently redirect to root when the website is accessed with mysite.com/index.php with no query string parameters?

0 comments
Message:


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.



My Answer:
To achieve the desired redirect behavior without affecting the query string parameters, you can modify your .htaccess rules as follows:


RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.php$ / [R=301,L]


This rule checks if the query string is empty and only redirects to the root if the index.php file is accessed without any query string parameters. This way, any query string parameters will not be affected and the site should function properly.

Rate this post

4 of 5 based on 1976 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us