Search code examples
apache.htaccesshttp-redirectmod-rewriteurl-rewriting

301 Redirect is Not Working with Index File


I'm trying to 301 Redirect the Index.php to another URL

Here's my htaccess codes

Options +FollowSymLinks
RewriteEngine on

Redirect 301 /index.php /en1

RewriteRule en1 index.php [NC]

My domain for example is

www.mysite.com 

I want this to work as

www.mysite.com/en1 

In the URL bar its showing the new URL but browser says

The page isn’t redirecting properly

How to fix this 301 Redirect?


Solution

  • With your shown samples please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.

    Options +FollowSymLinks
    RewriteEngine ON
    
    ##Implement http ---> https rules here.
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    ##External redirect rules here.
    RewriteRule ^/?$ /en5 [R=301,L]
    
    ##Internal rewrite to index.php rules here.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^en5/?$ index.php [L]
    

    OR If you want to write a Generic rule for rewriting each non-existing file to index.php then try following:

    Options +FollowSymLinks
    RewriteEngine ON
    
    ##Implement http ---> https rules here.
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    ##External redirect rules here.
    RewriteRule ^/?$ /en5 [R=301,L]
    
    ##Internal rewrite to index.php rules here.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ index.php [L]
    

    NOTE: Use either of rules set one at a time only.