apache mod_rewrite 在 url 中有空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/410811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mod_rewrite with spaces in the urls
提问by nickf
I need to set up some RewriteRules to redirect a URL which has a space in it. I've tried this:
我需要设置一些 RewriteRules 来重定向一个包含空格的 URL。我试过这个:
RewriteRule ^article/with%20spaces.html$ /article/without_spaces.html [R=301,L]
... but it doesn't work. Putting in a space instead of %20 causes a 500 Internal Server Error. How do I add a space?
......但它不起作用。放入空格而不是 %20 会导致 500 内部服务器错误。如何添加空格?
回答by Beau Simensen
Try putting a \ in front of your space to escape it.
尝试在您的空间前面放一个 \ 以逃避它。
RewriteRule ^article/with\ spaces.html$ /article/without_spaces.html [R=301,L]
回答by Rich Adams
You can just escape the space with a \
你可以用 \ 来逃避空间
RewriteRule ^article/with\ spaces.html$ /article/without_spaces.html [R=301,L]
回答by Denia
If you want to avoid the complexity of escaping each space (e.g. if you plan to have this file automatically generated), you can simply use quotes:
如果您想避免转义每个空格的复杂性(例如,如果您计划自动生成此文件),您可以简单地使用引号:
RewriteRule "^article/with spaces.html$" /article/without_spaces.html [R=301,L]
Furthermore, these quotes can be used to encase any one expected argument:
此外,这些引号可用于包含任何一个预期的参数:
RewriteRule "^article/with spaces.html$" "/article/without_spaces.html" [R=301,L]
回答by Abraham Kifle
RewriteRule ^article/with[\ |%2520]spaces.html$ /article/without_spaces.html [R=301,L]
The first option replaces a space while the second replaces hard coded %20 in the url.
第一个选项替换一个空格,而第二个选项替换 url 中的硬编码 %20。
回答by nickf
Ah, I've found a solution: use the regex style to show a space:
啊,我找到了一个解决方案:使用正则表达式样式来显示空格:
RewriteRule ^article/with\sspaces.html$ ...
Though, I suspect that this would match all the other whitespace characters too (tabs, etc), but I don't think it's going to be much of a problem.
虽然,我怀疑这也会匹配所有其他空白字符(制表符等),但我认为这不会有太大问题。

