Htaccess重定向问题

时间:2010-08-19 19:33:12

标签: .htaccess

我有一个用PHP编写的网站。

地址如下:
http://www.site.com/page.php

我想要任何请求:
www.site.com/page.php

www.site.com/page

转到:
www.site.com/page /

(并强制使用www。)

任何人都可以帮助我吗?

4 个答案:

答案 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)

蒂姆的答案是最接近的,但它也会为图像和CSS添加尾部斜杠...

所以接受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

哪个应该有用!