mod_rewrite在url中有空格

时间:2009-01-04 10:42:39

标签: apache url mod-rewrite

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

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

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

5 个答案:

答案 0 :(得分:56)

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

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

答案 1 :(得分:12)

你可以用\

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

答案 2 :(得分:9)

如果你想避免逃避每个空间的复杂性(例如,如果你打算自动生成这个文件),你可以简单地使用引号:

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

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

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

答案 3 :(得分:8)

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

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

答案 4 :(得分:1)

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

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

虽然,我怀疑这也会匹配所有其他空格字符(标签等),但我认为这不会是一个大问题。