RewriteRule用于查询字符串到静态URL

时间:2015-08-04 11:37:41

标签: regex apache .htaccess mod-rewrite

我有一个基于标签的搜索按钮链接到mydomain.com/product/search/?tag=hoodies,我想在浏览器中重写以显示为" mydomain.com/hoodies/(但仍然拉该原始网址的内容。)

这是一种纯粹的美学举动,我尝试过:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^tag=tanks
RewriteRule ^product/search/$ /tanks/ [L,QSA,NC]

对我有用,有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您可以在DOCUMENT_ROOT/.htaccess文件中使用此代码:

RewriteEngine On
RewriteBase /

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+product/search/\?tag=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L,NE]

# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ product/search/?tag=$1 [L,QSA]