Search code examples
regexapache.htaccessmod-rewritehttp-status-code-301

how to redirect with htacess when there are rewrite rules


I've got the following problem:
I need to redirect (301) from /index.php to homepage but there are rewrite rules:

RewriteCond %{REQUEST_URI} (/|\.htm|\.php|\.html|/[^.]*)$  [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php

And there is a loop of redirects when I add line:

Redirect 301 /index.php http://example.com/

How can I do it without changing the rewrite rules? .htaccess is not clear enough for me


Solution

  • Have your rule like this:

    RewriteCond %{THE_REQUEST} /index\.php [NC]
    RewriteRule ^(.*?)index\.php$ /$1 [L,R=301,NC,NE]
    
    RewriteCond %{REQUEST_URI} (\.html?|\.php|/[^.]*)$  [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ index.php [L]