Sarath Sarath - 3 months ago 9
Linux Question

.htaccess redirection fails when slash in index.php/

am having the below code in my .htaccess file,

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^index\.php$ / [L,R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /home
RewriteRule ^home$ / [L,R=301]


it successfully redirects when
www.domainname.com/index.php
to
www.domainname.com
.

But it fails to redirect when URL given like
www.domainname.com/index.php/

(a URL with slash after index.php)

Answer

Have it this way by making trailing slash optional in both the rules:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /+index\.php [NC]
RewriteRule ^index\.php/?$ / [L,R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /home [NC]
RewriteRule ^home/?$ / [L,NC,R=301]
Comments