0

我的重写规则几乎可以工作,但仍然存在一些问题。这是代码:

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

如何将该 - 符号转换为 + 符号?

4

2 回答 2

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]

当我转到类似的 URL 时mydomain.com/tag/some-thing-else-lots-of-dashes/,我会被重定向到mydomain.com/s=some+thing+else+lots+of+dashes&search=GA

于 2012-09-20T15:57:06.200 回答
0

看起来您的正则表达式正在从其集合中的传入 URL 中提取斜杠,尝试将其移出并使其成为可选,我还让您的选择器不那么贪婪:

RewriteRule ^tag/(.*?)/?$ /?s=$1&search=GA [L,R=301]
于 2012-09-20T13:13:52.157 回答