1

我正在尝试使用 mod_rewrite 提供不同的网址,但无论我尝试什么,它都不起作用。

一个示例网址是 http://www.site.com/country/tours/dynamic-part/?&city=new-york,los-angeles

我正在尝试使用 .htaccess 将 url 更改为:

http://www.site.com/country/tours/dynamic-part/new-york,los-angeles

RewriteEngine on
RewriteBase /

RewriteCond %{QUERY_STRING} (^|&)city=([^&]*)(&|$)
RewriteRule ^country\/tours\/([a-zA-Z0-9]*)\/.+city=([^\/]*)$ http://www.site.com/country/tours/$1/$2 [L,R=301]

RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ /index.php/$1 [L]

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

有任何想法吗?我虽然我很接近但不再是了:/

4

1 回答 1

2

RewriteRule 与查询字符串不匹配,请参阅 http://httpd.apache.org/docs/current/mod/mod_rewrite.html#what_is_matched

所以.+city规则的一部分永远不会匹配。

这应该可以工作...

RewriteCond %{QUERY_STRING} (^|&)city=([^&]*)(&|$)
RewriteRule ^country\/tours\/([a-zA-Z0-9]*)\/ http://www.site.com/country/tours/$1/%2 [L,R=301]

替代可以读回 RewriteCond 模式的引用。

于 2012-11-20T11:13:28.643 回答