开发者

Htaccess redirect question

I have a site I've written in php.

The addresses are as follows:

http://www.site.com/page.php

I'd like any request to:

www.site.com/page.php

or

www.site.com/page

开发者_如何学JAVA

To go to:

www.site.com/page/

(And force a www.)

Can anyone help me?


For domain :

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

For your first directory :

RewriteRule ^/([^/]*)(\.php)? /$1/


RewriteRule ^page.php$ http://www.site.com/page/ [QSA,L]
RewriteRule ^page$ http://www.site.com/page/ [QSA,L]

Maybe this will work.


This should do what you wanted:

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


Tim's answer is the closest, but it also does adds the trailing slash to images and css...

So taking Tim's answer, and slightly editing it gives us:

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

Which should work!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜