Kevin
Kevin

Reputation: 1725

htaccess rewrite index.php to trailing slash

Our site, example.com, has a bunch of directories that used to have links like this:

example.com/product-line-1/index.php
example.com/product-line-2/index.php

Except, more often it would be:

example.com/product-line-1

OR

example.com/product-line-1/

I realize this is a common inconsistency/problem and we should've been more consistent when we built the site way back when.

However, we've picked a sytax to use and it no index.php, but use trailing slash

example.com/product-line-1/

We've normalized the links to always be example.com/product-line-1/ and have some htaccess to add the trailing slash when there isn't one.

The last bit is to 301 redirect any example.com/directory/index1.php to example.com/directory/ to preserve any link juice those links may have.

The htaccess is quite long with a ton of rules, but here is an excerpt of the more relevant ones:

## Enables gzip compression 
<IfModule mod_deflate.c> 
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript 
SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|iso|tar|bz2|sit|rar|png|jpg|gif|jpeg|flv|swf)$ no-gzip dont-vary 
BrowserMatch ^Mozilla/4 gzip-only-text/html 
BrowserMatch ^Mozilla/4\.[0678] no-gzip 
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html 
Header append Vary User-Agent env=!dont-vary 
</IfModule> 

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##


RewriteEngine on

DirectoryIndex index.php index.html index.htm
RewriteCond %{REQUEST_URI} !^/images/.*

Can I add a line of htaccess to 301 redirect the /index.php to /? Is this a bad idea since it would impact all directories?

Alternatively, I could add a line of htaccess for each /product-directory but this would be several hundred lines.

Thanks in advance!

Upvotes: 2

Views: 1358

Answers (1)

Jonan
Jonan

Reputation: 2536

Use this:

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

Upvotes: 2

Related Questions