在htaccess中重定向动态地址

时间:2015-09-02 10:05:12

标签: php apache .htaccess mod-rewrite redirect

我希望有人可以帮助我,

我有一组看起来像这样的页面:

www.example.com/country/city/county/business-name/listing-id/contact.htm

和另一组看起来像这样的页面:

www.example.com/country/city/county/business-name/listing-id/index.htm

唯一不同的是动态网址末尾的联系人和索引词。

当然每个页面都不同,其他城市,国家等等。

我想要的是重定向所有

www.example.com/country/city/county/business-name/listing-id/contact.htm

www.example.com/country/city/county/business-name/listing-id/index.htm

动态。

这是我今天的代码,但它不起作用:

RewriteRule ^(.*).ht$ city.php?c=$1

RewriteRule ^(.*)/(.*)/index.html$ bizlist.php?state=$1&c=$2&first=0

RewriteRule ^(.*)/(.*)/index-(.*).html$ bizlist-alpha.php?state=$1&c=$2&alpha=$3&first=0

RewriteRule ^(.*)/(.*)/index-(.*)-(.*).html$ bizlist-alpha.php?state=$1&c=$2&alpha=$3&first=$4

RewriteRule ^(.*)/(.*)/businesses-(.*).html$ bizlist.php?state=$1&c=$2&first=$3

RewriteRule ^(.*)/(.*)/(.*)-County/(.*)/(.*)/index.htm$ business.php?s=$1&telephone=$5&c=$2
RewriteRule ^(.*)/(.*)/(.*)-County/(.*)/(.*)/index.htm$ ^(.*)/(.*)/(.*)-County/(.*)/(.*)/index.htm$

RewriteRule ^(.*)/(.*)/(.*)-County/(.*)/(.*)/$ business.php?s=$1&telephone=$5&c=$2

1 个答案:

答案 0 :(得分:1)

这应该做:

RewriteRule (.*)/contact.htm$ $1/index.htm [R,L]

当cat {.*执行时,不要使正则表达式超特定。你只对最后一部分感兴趣,所以只匹配最后一部分。当然,如果您的其他网址以contact.htm结尾,并且您不想重定向,则需要根据需要使正则表达式更具体。

还要注意规则的顺序很重要;您需要在任何其他重写发生之前插入此规则,这将导致它不再匹配。