如何处理 .htaccess 中的多个空格分隔的单词?
我在 .htaccess 中有以下代码,它适用于名称只有一个单词的页面。但对于由多个单词组成的页面名称,它会失败。
RewriteEngine on
RewriteRule ^(.*)/$ /index.php?page=$1 [L]
我使用结尾斜杠来表示 url 需要重写,以避免弄乱真实的文件名。
该问题可能与空格被 url 编码为 %20
有关。
关于如何更改 regex 或 .htaccess 一般有什么建议吗?
I have the following code in .htaccess and it works for pages which have a name of just one word. But it fails for page names consisting of more than one word.
RewriteEngine on
RewriteRule ^(.*)/$ /index.php?page=$1 [L]
I use the ending slash to indicate that the url needs to be rewritten, to avoid messing up real filenames.
The problem might have to do with the fact that spaces are url encode to %20
.
Any suggestion on how to alter the regex or .htaccess in general?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
RewriteRule 文档 包含您可以设置的标志列表。在这样的情况下,它总是值得一读。
看来您可能需要 B 标志:
B 标志告诉 Apache 在替换的反向引用中转义非字母数字字符,这显然是您想要的。
此外,如 Michael 所说,添加上述
RewriteCond
提供了一种更安全的方法,可以避免重写所请求的真实文件名(例如图像)的 URL。The RewriteRule documentation includes a list of flags that you can set. It's always worth reading in cases like this.
It seems that you may need the B flag:
The B flag tells Apache to escape nonalphanumeric characters in substituted backreferences, which apparently is what you want.
Also, adding the
RewriteCond
s as above provides a safer way to avoid rewriting the URLs of real file names (such as images) that are being requested, as Michael says.