【问题标题】:mod_rewrite with spaces in the urls网址中带有空格的 mod_rewrite
【发布时间】:2023-04-02 04:30:01
【问题描述】:

我需要设置一些 RewriteRules 来重定向其中包含空格的 URL。我试过这个:

RewriteRule ^article/with%20spaces.html$ /article/without_spaces.html [R=301,L]

...但它不起作用。放入空格而不是 %20 会导致 500 内部服务器错误。如何添加空格?

【问题讨论】:

    标签: apache url mod-rewrite


    【解决方案1】:

    尝试在你的空间前面放一个 \ 来逃避它。

    RewriteRule ^article/with\ spaces.html$ /article/without_spaces.html [R=301,L]
    

    【讨论】:

    • 干杯!我应该认为它会像那样简单。
    【解决方案2】:

    你可以用 \

    转义空格
    RewriteRule ^article/with\ spaces.html$ /article/without_spaces.html [R=301,L]
    

    【讨论】:

      【解决方案3】:

      如果您想避免转义每个空格的复杂性(例如,如果您打算自动生成此文件),您可以简单地使用引号:

      RewriteRule "^article/with spaces.html$" /article/without_spaces.html [R=301,L]
      

      此外,这些引号可用于包含任何一个预期参数:

      RewriteRule "^article/with spaces.html$" "/article/without_spaces.html" [R=301,L]
      

      【讨论】:

      • 这是最好的答案
      【解决方案4】:
      RewriteRule ^article/with[\ |%2520]spaces.html$ /article/without_spaces.html [R=301,L]
      

      第一个选项替换一个空格,而第二个替换 url 中硬编码的 %20。

      【讨论】:

      • 这应该得到最高票数。可惜!时间取决于投票。
      【解决方案5】:

      啊,我找到了解决方案:使用正则表达式样式显示空格:

      RewriteRule ^article/with\sspaces.html$ ...
      

      不过,我怀疑这也会匹配所有其他空白字符(制表符等),但我认为这不会有太大问题。

      【讨论】:

        猜你喜欢
        • 2012-10-18
        • 2013-08-10
        • 1970-01-01
        • 2012-08-09
        • 2014-08-10
        • 1970-01-01
        • 2012-06-02
        • 1970-01-01
        相关资源
        最近更新 更多