[英]Apache rewrite rules for creating user friendly urls
我是相对较新的重写者,无法完成以下工作。 (显然,我的域不是测试站点,但不希望我的域名公开,因为它尚未完成。)
我想重写testsite / fishing / region / region.php?region = fife
至:
testsite / fishing / fife.php
然后要重写testsite / fishing / region / fishery / fishery.php?url = goldenloch
至
testsite / fishing / fife / goldenloch.php
我正在使用以下规则
RewriteRule ^fishing/([^/]*)\.php$ /fishing/region/region.php?region=$1 [L]
RewriteRule ^fishing/([^/]*)/([^/]*)\.php$ /fishing/region/fishery/fishery.php?url=$2®ion=$1 [L]
每条规则都可以独立工作,但只有最后一项可以结合使用。 我添加了[L]标志,我认为该标志应停止满足该条件的任何其他重写规则。 但是,这仍然行不通。
原来我需要做
RewriteRule ^fishing/([^/]*)/([^/]*) fishery.php?url=$2®ion=$1 [L]
RewriteRule ^fishing/([^/]*) region.php?region=$1 [L]
首先使用较长的查询,并且效果很好。
我尝试了一下,它似乎起作用了:
RewriteRule ^/fishing/([^/]*)\.php$ /fishing/region/region.php?region=$1 [R,L]
RewriteRule ^/fishing/([^/]*)/([^/]*)\.php$ /fishing/region/fishery/fishery.php?url=$2®ion=$1 [R,L]
与您的唯一区别(我认为)是前导/
,我使用重定向( R
)只是为了查看它是否有效。 您应该能够删除R
以便用户在浏览器中看不到真实的URL。
以防万一,我建议使用命令行工具(例如curl
),而不要使用浏览器进行测试,以免造成任何缓存或其他麻烦:
curl -vv 'http://localhost/fishing/fife/goldenloch.php'
您将在服务器的响应中看到Location
标头,即工作中的重定向。 就我而言,我看到:
Location: http://localhost/fishing/region/region.php?region=fife
和
Location: http://localhost/fishing/region/fishery/fishery.php?url=goldenloch®ion=fife
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.