2012-09-20 77 views
0

我的重写规则几乎可行,但仍然存在一些问题。下面是代码:301重定向多个页面

RewriteEngine on 
RewriteRule ^tag/(.*)$ /?s=$1&search=GA [L,R=301] 

第一现在的问题是重定向链接:

mydomain.com/?s=tag/&search=GA 

我怎样才能摆脱第二斜线?

现在第二问题...当标签包含超过1个字(例如标签营销技巧)重定向是:

mydomain.com/?s=marketing-tips/&search=GA 

如何转换是 - 符号为+符号?

+0

不是专家,对这个如此不张贴作为一个答案,只是一个建议 - 也许你需要使用反斜线或任何转义字符在正则表达式中使用&来逃避?至于你的第二个问题,我不能建议。 – jammypeach

回答

0

它看起来像你的正则表达式是拿起从输入网址在它的采集,尝试移动它,并使它可选的斜杠,我也做你的选择贪心不足:

RewriteRule ^tag/(.*?)/?$ /?s=$1&search=GA [L,R=301] 
+0

谢谢!不,它适用于包含1个单词的标签。但现在我想为包含2个或更多字的标签工作。任何关于如何将该符号转换为符号的建议? – OscarHelm

+0

@OscarHelm看看这个问题http://stackoverflow.com/questions/4144121/mod-rewrite-replace-with ...你也可以用http://regexplanet.com或http://测试你的正则表达式regexr.com – JKirchartz

1

试试这个:

# getting rid of trailing slash: 
RewriteRule ^tag/(.*?)/?$ /?s=$1&search=GA [L] 

# change "-" with "+": 
RewriteCond %{QUERY_STRING} ^s=([^&]+)-([^&]+)&(.*) 
RewriteRule ^(.*)$ /$1?s=%1+%2&%3 [L,NE] 

# if there's no more "-", redirect: 
RewriteCond %{QUERY_STRING} ^s=([^&-]+)(&|$) 
RewriteRule ^(.*)$ /$1 [L,R=301] 

当我去一个像mydomain.com/tag/some-thing-else-lots-of-dashes/一个网址,我重定向到mydomain.com/s=some+thing+else+lots+of+dashes&search=GA