Apache mod_rewrite:这些简单的RewriteRule可以改进吗?以及建议!

发布于 2024-09-10 05:54:01 字数 697 浏览 1 评论 0原文

我终于开始了解 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许你一世情深 2024-09-17 05:54:01
  1. 使用 ? 量词而不是 * 并且不需要转义替换 URL:

    RewriteRule ^folder_name/?$ http://example.com/some/path/some_page.html [R=301,L]
    
  2. 您可能需要考虑 HTTP 1.0 请求缺少 Host 标头字段。另一个有用的扩展是考虑 HTTPS:

    RewriteCond %{HTTP_HOST} !^(|example\.com)$
    RewriteCond %{HTTPS} ^on(s)|
    RewriteRule ^ http%1://example.com%{REQUEST_URI} [R=301,L]
    
  1. Use the ? quantifier instead of * and you don’t need to escape the substitution URL:

    RewriteRule ^folder_name/?$ http://example.com/some/path/some_page.html [R=301,L]
    
  2. 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:

    RewriteCond %{HTTP_HOST} !^(|example\.com)$
    RewriteCond %{HTTPS} ^on(s)|
    RewriteRule ^ http%1://example.com%{REQUEST_URI} [R=301,L]
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文