.htaccess快速网址修复

时间:2013-12-08 11:04:11

标签: php .htaccess

我有以下.htaccess脚本:

RewriteEngine On
RewriteBase /

# remove enter code here.php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]

# remove index
RewriteRule (.*)/index$ $1/ [R=301]

# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]

# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

RewriteCond %{HTTP_HOST} ^loaidesign.co.uk [NC]
RewriteRule ^(.*)$ http://www.loaidesign.co.uk/$1 [R=301,L]

它会创建如下的网址:http://www.loaidesign.co.uk/projects.php?project=Wix_Websites无论如何都要删除.php?project=并将其替换为/,因此它只显示www.loaidesign.co.uk/projects/Wix_Websites?另外如何将“_”改为“ - ”?

3 个答案:

答案 0 :(得分:0)

我没有测试过,但我相信那会是:

RewriteBase /projects/
RewriteRule ^/([^/]+)/([^/]+)$  $1.php?project=$2

编辑:在this website上测试,它确实有效。

答案 1 :(得分:0)

在最后的地方

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

会将projects / *中的所有内容重写为projects.php?project = *

答案 2 :(得分:0)

RewriteCond %{REQUEST_URI}  ^/projects\.php$
RewriteCond %{QUERY_STRING} ^project=([a-z_]*)$ [NC]
RewriteRule ^(.*)$ http://www.loaidesign.co.uk/project/%1 [L]

这会产生http://www.loaidesign.co.uk/project/Wix_Websites之类的网址。在http://htaccess.madewithlove.be/上测试并改编自http://www.simonecarletti.com/blog/2009/01/apache-query-string-redirects/

[编辑]经过无数次评论,我认为你想要的恰恰相反。您希望在页面上拥有漂亮的URL(如/project/foo),然后将其转换为内部重新定位(?projects.php?project=foo):

RewriteCond %{REQUEST_URI} !projects.php
RewriteRule ^project/(.*)$ projects.php?project=$1 [L]

[edit2]与我的答案的第一部分一起,你可以实现你想要的目标:

RewriteCond %{REQUEST_URI}  projects\.php
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^project=([a-z_]*)$ [NC]
RewriteRule ^(.*)$ project/%1 [R=302,L]

RewriteCond %{REQUEST_URI} !projects.php
RewriteRule ^project/(.*)$ projects.php?project=$1 [L]

它并不完美,因为如果输入了旧样式的URL,它会将查询字符串附加到经过修饰的URL,但我会将其留给您。 ; - )