.htaccess用参数重写URL

时间:2015-05-21 16:54:32

标签: html .htaccess

我希望从示例中更改我的网址:

www.website.com/?page=about
www.website.com/?page=info
www.website.com/?page=contact

到此:

www.website.com/about
www.website.com/info
www.website.com/contact

这应该是我希望的一条规则。我一直在环顾四周,但我看到人们分别做每次事件。 所以唯一的URL参数是“page”,网站只有一个深度。应该很容易吗? :)

2 个答案:

答案 0 :(得分:2)

这是一个非常基本的常见问题。这在你的root中的.htaccess中。

RewriteEngine On
RewriteBase /

#prevent anyone from using the old urls
RewriteCond %{THE_REQUEST} [A-Z]{3,}\ /+\?page=([^&\ ]+) [NC]
RewriteRule ^ /%1? [R=301,L]

#provide pretty URL's
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /?page=$1 [L]

答案 1 :(得分:2)

您可以在DOCUMENT_ROOT/.htaccess文件中使用此代码:

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} \s/+\?page=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)/?$ ?page=$1 [L,QSA]