Htaccess regex problem with redirection

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 the .com website without www, it will redirect to www.site.com/.php instead of www.site.com.

Any thoughts?

Thank!

+3
source share
4 answers

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

Thank you for your help.

+1
source

Perhaps this is the browser cache from the old .htaccess? Try clearing the cache or using a different browser (or perhaps viewing Priavte).

+1
source

, SCRIPT_FILENAME "/", ! -f, .

Since the / character is a character caught by a regular expression, it will rewrite it at www.site.com/.php

+1
source

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

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

All Articles