htaccess mod_rewrite搞清楚如何读取变量的连字符

时间:2014-03-05 03:07:43

标签: regex apache .htaccess mod-rewrite

我想要做的就是创建一个漂亮的网址:

/portfolio/city-state/projectname.php

所以,例如,我可以

/portfolio/Mobile-AL/Johnsonsite.php

我的mod_rewrite规则工作得很好:

RewriteRule ^portfolio/([A-Za-z0-9_']+)-([A-Za-z0-9]+)/([A-Za-z0-9'-]+).php /portfolio-single.php?city=$1&state=$2&name=$3 [L]

但问题是我想尝试在城市名称中使用连字符而不是下划线,所以网址可能是:

/portfolio/New-Holland-PA/Johnsonsite.php
--or--
/portfolio/Salt-Lake-City-UT/Johnsonsite.php

无论如何都要从最右边/向左计数,比如首先拉出2个字母的状态并假设剩下的就是城市?或者你知道更好的方法吗?

我最诚挚的问候,谢谢! :)

1 个答案:

答案 0 :(得分:0)

您可以像这样调整匹配正则表达式的网址:

RewriteRule ^portfolio/(.+?)-([^-/]+)/([a-z0-9'-]+)\.php$ /portfolio-single.php?city=$1&state=$2&name=$3 [L,QSA,NC]