Apache mod_rewrite:这些简单的RewriteRule可以改进吗?以及建议!
我终于开始了解 Apache mod_rewrite。太棒了!
请查看以下内容:
1) 永久重定向 http://www.domain.com/folder_name/
(带或不带最后斜线以及带或不带 www
) 到 http://www.domain.com/some/path/some_page.html
RewriteRule ^folder_name[/]*$ "http\:\/\/domain\.com\/some\/path\/some_page.html" [R=301,L]
2) 永久将所有请求重定向到 www.domain.com...
到同一个路径和文件请求,但域中没有 www
RewriteCond %{HTTP_HOST} !^domain.com$
RewriteRule ^(.*)$ "http\:\/\/domain\.com\/$1" [R=301,L]
它们都按预期工作并完成其工作,我只是好奇是否有人在 mod_rewrite 方面比我更专家,可以给我一些建议,例如:“这样可能会更好......”,“如果......可能会有问题”等。
谢谢!
I started finally to understand Apache mod_rewrite. It's pretty GREAT!
Plz have a look at the followings:
1) Permanent redirects http://www.domain.com/folder_name/
(with or without final slash and with or without the www
) to http://www.domain.com/some/path/some_page.html
RewriteRule ^folder_name[/]*$ "http\:\/\/domain\.com\/some\/path\/some_page.html" [R=301,L]
2) Permanent redirects all requests to www.domain.com...
to same path and file request but without www
in domain
RewriteCond %{HTTP_HOST} !^domain.com$
RewriteRule ^(.*)$ "http\:\/\/domain\.com\/$1" [R=301,L]
They all work as expected and do their jobs, I'm simply curios if some guy, who is more expert than me in mod_rewrite, could give me some advises like: "it could be better in this way...", "there might be a problem if...", etc.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
?
量词而不是*
并且不需要转义替换 URL:您可能需要考虑 HTTP 1.0 请求缺少 Host 标头字段。另一个有用的扩展是考虑 HTTPS:
Use the
?
quantifier instead of*
and you don’t need to escape the substitution URL:You might want to consider HTTP 1.0 requests where the Host header field is missing. Another useful extension would be to take HTTPS into account: