.htaccess 302使用正则表达式替换重定向
问题描述:
我有搜索webstie用户在url中输入以下查询并执行搜索。.htaccess 302使用正则表达式替换重定向
http://www.example.com/?search=key word&type=sin
http://www.example.com/?search=key word
我做在页面的规范链接,以避免这些在goodle被索引。
<link rel="canonical" href="http://www.example.com/key-word/"/>
但是我需要添加一个302重定向以及对此。即
Redirect 302/http://www.example.com/key-word/ (key word is replaced spaces to hypen)
我该怎么做?
答
您可以在DOCUMENT_ROOT/.htaccess
文件中使用此代码:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?search=([^\s&]+) [NC]
RewriteRule^/%1? [R=302,L,NE]
## replace of space with -
# executes **repeatedly** as long as there are more than 1 spaces in URI
RewriteRule "^(\S*) +(\S* .*)$" /$1-$2 [L,NE]
# executes when there is exactly 1 space in URI
RewriteRule "^(\S*) (\S*)$" /$1-$2? [L,R=302,NE]
没有工作似乎被重定向到相同的URL – mahen3d 2014-11-24 02:30:01
有一个错字,现在是固定的。 – anubhava 2014-11-24 02:38:59
现在它重定向,但似乎添加像这样的查询字符串“xyz.com/key-word/?search=key word” – mahen3d 2014-11-24 02:42:33