将非 www 重定向到 www 不起作用

发布于 2024-10-20 00:09:12 字数 584 浏览 2 评论 0原文

我使用 virtualmin 设置了一个虚拟服务器,它没有创建 .htaccess 文件,所以我在 public_html 文件夹中创建了一个文件,并放入以下代码

RewriteEngine On
RewriteCond % ^megahotserved.com [NC]
RewriteRule ^(.*)$ http://www.megahotserved.com/$1 [L,R=301]

重新启动 apache,但没有效果,然后

<VirtualHost *:80>
    ServerName megahotserved.com
    Redirect permanent / http://www.megahotserved.com/
</VirtualHost>

在重新启动 apache 时在 httpd.conf 文件中 尝试火狐出现了一个错误

页面未正确重定向。

Firefox 检测到服务器正在以永远无法完成的方式重定向对此地址的请求。

我应该怎么办 ?

I set up a virtual server using virtualmin, it didn't create the .htaccess file so I created one in the public_html folder and put the following code

RewriteEngine On
RewriteCond % ^megahotserved.com [NC]
RewriteRule ^(.*)$ http://www.megahotserved.com/$1 [L,R=301]

restarted apache and no effect and then tried

<VirtualHost *:80>
    ServerName megahotserved.com
    Redirect permanent / http://www.megahotserved.com/
</VirtualHost>

in the httpd.conf file, when I restarted apache firefox came up with an error

The page isn't redirecting properly.

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

What should I do ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

半步萧音过轻尘 2024-10-27 00:09:12

您的代码是正确的,您只需遵循我引用的下一段 http: //httpd.apache.org/docs/current/mod/mod_rewrite.html

默认情况下,虚拟主机不会继承主服务器上下文中的 mod_rewrite 配置设置。要使主服务器设置应用于虚拟主机,您必须在每个部分中放置以下指令:

RewriteEngine 开启
RewriteOptions 继承

your code is correct, you just need to follow the next paragraph which I quoted from http://httpd.apache.org/docs/current/mod/mod_rewrite.html

By default, mod_rewrite configuration settings from the main server context are not inherited by virtual hosts. To make the main server settings apply to virtual hosts, you must place the following directives in each section:

RewriteEngine On
RewriteOptions Inherit
べ繥欢鉨o。 2024-10-27 00:09:12

似乎您没有与 www. 地址正确匹配的 VirtualHost,因此对 http://www.megahotserved.com/ 的请求正在访问同一个虚拟主机并进入循环重定向。所以重定向工作正常;您只需修复服务器配置的不同部分即可。

Seems like you don't have a VirtualHost that properly matches the www. address, so requests for http://www.megahotserved.com/ are hitting the very same vhost and getting into a circular redirect. So the redirect is working fine; you just have a different part of the server config to fix.

话少心凉 2024-10-27 00:09:12

同意上面的内容,还有一点补充:最好将非 www 重定向到 www,而不是重写,否则您将拥有整个网站的两个完整视图(“副本”);每个页面都有两个 URL,而不是一个规范 URL。这可能对搜索引擎和其他事物不利。

Agree with the above, and a small addition: it is better to redirect non-www to www rather than rewrite, otherwise you have two complete views ("copies") of your entire website; each page has two URLs, instead of one canonical one. This can be bad for search engines and other things.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文