我首先在我的网站上删除了.php扩展程序。然后我转发www到非www版本。但是有一个问题。
我的.htaccess文件如下所示:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^188\.166\.104\.194
RewriteRule (.*) http://example.com/$1 [R=301,L]
www使用.htaccess转发到非www。太棒了。但问题出在其他文件上。
我现在正在使用:http://example.com/contact代替:http://example.com/contact.php
但当您尝试打开http://www.example.com/contact时,.htaccess会将我转发给http://example.com/contact.php/
我该如何解决?
度过愉快的一天!
答案 0 :(得分:1)
更改规则的顺序和一些重构:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.example\.com|188\.166\.104\.194)$ [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteRule ^([^/]+)/$ $1.php [L]
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php [L]
确保在清除浏览器缓存后进行测试。