【问题标题】:htaccess replace character in query and redirecthtaccess 在查询和重定向中替换字符
【发布时间】:2013-10-20 21:12:24
【问题描述】:

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

site.com/abc_def/

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

我试过了

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

【问题讨论】:

  • RewriteRule ^([^/]+)/((.*)\_(.*))?$ /search.php?q=$1+$2 [R=301,L]
  • 当你尝试这样做时发生了什么?
  • /abc -> /search.php?q=abc+, /abc_def -> /search.php?q=abc_def+
  • 为什么开头有([^/]+)?提供有关示例输入 URL 和预期重写 URL 的更多详细信息。
  • 输入:site.com/abc_def/。重写:site.com/search.php?q=abc+def

标签: regex .htaccess redirect


【解决方案1】:

以下 2 条规则应该适合您:

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]

【讨论】:

  • 当我尝试“site.com/abc/”或“site.com/abc_def/”时,服务器返回内部服务器错误
  • 这是经过适当测试的代码。阅读 error.log 并在此处更新您看到的错误。
猜你喜欢
  • 2019-01-03
  • 2015-03-31
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多