Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 333
Tyler Featherston Avatar asked Mar 15 '26 02:03

Tyler Featherston


2 Answers

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

like image 124
Yaakov Shoham Avatar answered Mar 17 '26 00:03

Yaakov Shoham


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

like image 23
Trevor Dickson Avatar answered Mar 16 '26 22:03

Trevor Dickson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!