Ken
Ken

Reputation: 39

Regular expression for DirectoryIndex in htaccess

Sorry if I duplicate some discussions about .htaccess. but I didn't find a direct answer to my questions. Here they are: 1. I am trying to remove index.php from URLs in home directory and subdirectories. So far I've come to the following two rules (one for home directory and the other for subdirectories):

# removes index.php (in public_html)
RewriteRule ^index.php$ / [QSA,R]

# removes index.php (in all directories exept public_html)
RewriteRule ^(.*?)/index\.php /$1 [R=301,R]

Is there a way to make it with one common rule?

  1. I also have quite a few subdirectory-index-files with different prefixes ending with -index: bla-index, foo-index, prefix-index etc. I want to make them all work as DirectoryIndex using some kind of regular expression. The following rule removes prefix-index.php

    #removes prefix-index.php (in all directories exept public_html)
    RewriteRule ^(.*)/(.*)index.php /$1 [R=301,R]
    

That's exactly what I wanted but now I have the 403 error because htaccess says:

DirectoryIndex index.php

I've tried different regular expressions for DirectoryIndex with no success. Please help!

P.S. I can only use the htaccess not the httpd.conf

Upvotes: 1

Views: 301

Answers (1)

anubhava
anubhava

Reputation: 785128

You can remove index.php using this code in site root .htaccess:

DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On

RewriteCond %{THE_REQUEST} \s(.*)index\.php[?\s] [NC]
RewriteRule ^ %1 [L,R=301,NE]

Upvotes: 1

Related Questions