我有一个用PHP编写的网站。
地址如下:
http://www.site.com/page.php
我想要任何请求:
www.site.com/page.php
或
www.site.com/page
转到:
www.site.com/page /
(并强制使用www。)
任何人都可以帮助我吗?
答案 0 :(得分:1)
对于域名:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
第一个目录:
RewriteRule ^/([^/]*)(\.php)? /$1/
答案 1 :(得分:0)
RewriteRule ^page.php$ http://www.site.com/page/ [QSA,L]
RewriteRule ^page$ http://www.site.com/page/ [QSA,L]
也许这会奏效。
答案 2 :(得分:0)
这应该做你想要的:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/([^\s]*?)\.php [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/([^\s]*?[^/\s])\s [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST}/%1 ^(www\.)?(.*?)/?$
RewriteRule ^ http://www.%2/ [R=301,L]
# Assuming you want to write the request back to page.php too...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ $1.php
答案 3 :(得分:0)
所以接受Tim的回答,稍加编辑就会给我们:
RewriteEngine on
RewriteRule \.(css|jpe?g|gif|png)$ - [L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/([^\s]*?)\.php [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/([^\s]*?[^/\s])\s [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST}/%1 ^(www\.)?(.*?)/?$
RewriteRule ^ http://www.%2/ [R=301,L]
# Assuming you want to write the request back to page.php too...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ $1.php
哪个应该有用!