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

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

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

有帮助吗?

解决方案

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

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

其他提示

你可以用\

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

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

RewriteRule "^article/with spaces.html
RewriteRule "^article/with spaces.html<*>quot; "/article/without_spaces.html" [R=301,L]
quot; /article/without_spaces.html [R=301,L]

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

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

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

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

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

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top