htaccess替换查询和重定向中的字符

时间:2013-10-13 01:49:07

标签: regex .htaccess redirect

我需要在查询字符串中将'_'替换为'+'而不是重定向:

site.com/abc_def/

site.com/search.php?q=abc+def

我试过这个

RewriteRule ^([^/]+)/((.*)\_(.*))?$ /search.php?q=$1+$2 [R=301,L]

1 个答案:

答案 0 :(得分:1)

这些规则应该适合您:

RewriteEngine On

# first replace _ by + recursively
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^_]*)_(.*)$ /$1+$2 [L]

# once all _s are gone, rewrite to /search.php?q=<search>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^_]+)$ /search.php?q=$1 [L,QSA]