如何使用htaccess重定向不同的多个页面

时间:2017-03-26 05:01:53

标签: php apache .htaccess redirect

我成功地重定向了一页并且工作正常,但是当我对其他页面做同样的事情时它无法正常工作。使用htaccess重定向和设置url的最佳方法是什么,而不影响CSS,JS,IMG。 bellow是我用来重定向的代码。

RewriteCond $1 ^ 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ search.php?loc=$1&q=$2 [L,QSA]


# Redirect to SEO Friendly Url search page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(?:search\.php)?\?loc=([^\s]+)&q=([^\s]+)? [NC]
RewriteRule ^ /%1/%2? [R=301,L]
#RewriteRule ^([^/]*)/([^/]*)$ /search.php?loc=$1&q=$2 [L]

我的网址是

https://www.zesteve.com/search.php?loc=Guntur&q=manjunath-cake-shop

成功改为

https://www.zesteve.com/Guntur/manjunath-cake-shop

我不希望我的网址显示网页名称search.php

现在我想改变下面的网址

https://www.zesteve.com/vendor.php?city=Hyderabad&type=bakers-and-confectioners&vname=manjunath-cake-shop&vid=87411490256961

https://www.zesteve.com/Hyderabad/bakers-and-confectioners/manjunath-cake-shop/87411490256961

使用以下代码

# Redirect to SEO Friendly Url Vendor page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(?:vendor\.php)?\?city=([^\s]+)&type=([^\s]+)&vname=([^\s]+)&vid=([^\s]+)? [NC]
RewriteRule ^ /%1/%2/%3/%4? [R=301,L]

RewriteCond $1 ^ 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ vendor.php?city=$1&type=$2&vname=$3&vid=$4 [L,QSA]

我的工作正常吗?

如果有任何身体不明白请发表评论

1 个答案:

答案 0 :(得分:2)

在上一次重写规则中,您只捕获一个值,但在目标中使用$1,$2,$3,$4

将规则更改为:

# Redirect to SEO Friendly Url Vendor page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(?:vendor\.php)?\?city=([^\s&]*)&type=([^\s&]*)&vname=([^\s&]*)&vid=([^\s&]*) [NC]
RewriteRule ^ /%1/%2/%3/%4? [R=301,NE,L]

# Redirect to SEO Friendly Url search page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(?:search\.php)?\?loc=([^\s&]*)&q=([^\s&]*)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/?$ vendor.php?city=$1&type=$2&vname=$3&vid=$4 [L,QSA]

RewriteRule ^([\w-]+)/([\w-]+)/?$ /search.php?loc=$1&q=$2 [L,QSA]