Search code examples
.htaccessurl-rewriting

htaccess RewriteUrl parameters not checked


I need to rewrite url:

mydomain.com/index.php -> mydomain.com

I tried to use:

RewriteRule ^index.php$ /$1 [R=301,L]

The problem - using this url rewrite mydomain.com/index.php? (take a look at a question mark at the end) is redirected too. Need to rewrite url just for exact match of mydomain.com/index.php without nothing added at the end (parameters etc).

Complementary explanation:

mydomain.com/index.php?abc dont have to be rewritten

just mydomain.com/index.php -> mydomain.com


Solution

  • You can do this using a RewriteCond with THE_REQUEST:

    RewriteEngine On
    
    RewriteCond %{THE_REQUEST} \s/+index\.php\s [NC]
    RewriteRule ^ / [L,R=302]
    

    Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

    THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of other rewrite directives. Example value of this variable is GET /index.php?id=123 HTTP/1.1