mod_rewrite with spaces in the urls

删除回忆录丶 提交于 2019-12-28 12:45:28

问题


I need to set up some RewriteRules to redirect a URL which has a space in it. I've tried this:

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?


回答1:


Try putting a \ in front of your space to escape it.

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



回答2:


You can just escape the space with a \

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



回答3:


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]



回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/410811/mod-rewrite-with-spaces-in-the-urls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!