Othe12sid3
Othe12sid3

Reputation: 35

Hiding index.php from ALL subdirectories using .htaccess

So I'm building an ecommerce website using a file structure suggested to me, it looks like this:

http://www.sample.com/index.php
http://www.sample.com/department/index.php
http://www.sample.com/department/men/index.php

Basically I want to get rid of all the index.php filenames in the url, so it looks like this:

http://www.sample.com
http://www.sample.com/department
http://www.sample.com/department/men

I've achieved this for my homepage with adding this code in .htaccess:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

But, is there any way to apply this rule to every subdirectory in my site with just editing the root .htaccess file? I'm guessing it can be done by adding this rule in .htaccess files in every directory but I'd rather just have one.

Also, is this even a good way to structure a website? I am not particularly fond of it as there are so many index.php files but I'm not sure of a better alternative...

Upvotes: 3

Views: 725

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

Try putting these rules above the rules that you have in your htaccess file in the document root:

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

Upvotes: 1

Related Questions