Tyler Featherston
Tyler Featherston

Reputation: 43

Htaccess Regex problem with redirect

When I use this in my htaccess file:

RewriteCond %{HTTP_HOST} ^site\.com$ [NC]
RewriteRule ^(.*)$ http://www.site.com/ [R=301]

RewriteCond %{SCRIPT_FILENAME} !-f   
RewriteRule ^(.+)$ $1.php [NC,L] 

If you go to site.com without www it redirects to www.site.com/.php instead of www.site.com.

Any thoughts?

Thanks!

Upvotes: 3

Views: 117

Answers (4)

Tyler Featherston
Tyler Featherston

Reputation: 43

I ended up fixing it by adding $1 to RewriteRule ^(.*)$ http://www.site.com/$1 [R=301,L]

Thanks for all your help.

Upvotes: 1

gaRex
gaRex

Reputation: 4215

Try to add "last" to rule by adding ",L":

RewriteRule ^(.*)$ http://www.site.com/ [R=301,L]

Upvotes: 1

Trevor Dickson
Trevor Dickson

Reputation: 11

I think at the root SCRIPT_FILENAME would be "/", which would not be considered a valid file and trip the !-f causing the rule to be applied.

Since the / is a character caught by the regex it will rewrite it to www.site.com/.php

Upvotes: 1

Yaakov Shoham
Yaakov Shoham

Reputation: 10538

Maybe it's browser's cache from your old .htaccess? Try to empty the cache, or use other browser (or maybe Priavte Browsing).

Upvotes: 1

Related Questions