Erik Gonzalez
Erik Gonzalez

Reputation: 23

stop .htaccess from redirecting subfolder/index.php to root

I have an issue I can't seem to debug. I have tried many solutions and no luck.

When I access a URL with subfolder/index.php I get redirected back to the root page. If I access another page in that subfolder it works fine. I am not sure what is causing this on my code. Here is what I have.

Options +FollowSymLinks -MultiViews 
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule (.*) http://www.domain.com/$1 [R=301,L] 

RewriteCond %{HTTP_HOST} ^www.domain\.com
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301] 

# remove index
RewriteRule (.*)index$ $1 [R=301]

# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]

# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

Any suggestions? Thanks!

Upvotes: 2

Views: 1774

Answers (1)

anubhava
anubhava

Reputation: 785286

Keep your rules like this:

Options +FollowSymLinks -MultiViews 
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L,NE] 

RewriteCond %{HTTP_HOST} ^www.domain\.com
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]

# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule ^(.+?)/$ $1 [R=301,L,NE]

# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1.php [L]

Upvotes: 1

Related Questions