简体   繁体   English

HTTP:// WWW。 到http://重定向根本不起作用

[英]http://www. to http:// redirect not working at all

I want a rather simple feature, I want my website to work when someone puts a www. 我想要一个相当简单的功能,我希望当有人输入www时我的网站能够正常工作。 before my domain name. 在我的域名之前。 My preference is to redirect all http://www. 我的偏好是重定向所有http://www. requests to http:// without anything complicated. 请求到http://没有任何复杂的过程。

I have tried two methods to this and neither works desirably. 我已经尝试了两种方法,但都无法正常工作。 The first option was to simply add this following rewrite engine command in the site's .htaccess (which, for the record, I confirmed was working) 第一种选择是简单地在网站的.htaccess添加以下重写引擎命令(根据记录,我确认它正在运行)

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

I tried numerous variations of this code, and none of them do a thing. 我尝试了此代码的多种变体,但没有一个可以做任何事情。 Even after resetting my browser cache, accessing through Tor, if I enter my website with the www. 即使重置浏览器缓存后,如果我使用www.进入我的网站,也可以通过Tor访问www. prefix then it simply doesn't load and Firefox gives me a "server not found" error. 前缀,则它根本无法加载,并且Firefox给我一个“找不到服务器”错误。

The second thing that I have tried is to remove all rewrite codes and simply add the DNS record A, with the host www that points to my IP address. 我尝试的第二件事是删除所有重写代码,仅添加DNS记录A,并将主机www指向我的IP地址。 After waiting a little bit, the website was now working! 稍等片刻后,该网站现在可以正常工作! However, it was incredibly inconsistent, because if I were to access www.example.com then it would remove the www. 但是,这是非常不一致的,因为如果我要访问www.example.com ,它将删除www. , but if I went to www.example.com/page.html then it wouldn't remove it (but it'd still work). ,但是如果我转到www.example.com/page.html它不会将其删除(但仍可以使用)。 Another weird issue is that if I typed www.example.com/folder/page , it'd send me to www.example.comfolder/page . 另一个奇怪的问题是,如果我输入www.example.com/folder/page ,它将把我发送到www.example.comfolder/page

Does anyone have any advice on what should I do or what I could be doing wrong? 有人对我应该做什么或我可能做错了什么有任何建议吗?

This should work. 这应该工作。

RewriteEngine On 
RewriteCond %{HTTP_HOST} !^example\.com 
RewriteRule (.*) http://example.com/$1 [R=301,L] 

(This is not necessarily an answer, but my reputation is not yet sufficient to post a comment.) (这不一定是答案,但是我的声誉还不足以发表评论。)

The previous comment looks like it should do the job, so something unexpected is going on with the rewrite. 前面的注释看起来应该可以完成工作,因此重写时发生了意外情况。

You might try enabling rewrite logging at a low level to see what is going on with the rewrite rule. 您可以尝试在较低级别启用重写日志记录,以查看重写规则的情况。 Increase logging level by one level if the issue is not revealed. 如果未发现问题,则将日志记录级别提高一级。 Note that logging mod_rewrite actions can produce really big logs, so it is prudent to try this on a testbed or a relatively idle host. 请注意,记录mod_rewrite操作可能会产生非常大的日志,因此,请在测试平台或相对空闲的主机上尝试这样做。

# Enable mod_rewrite
RewriteEngine On

# RewriteLogLevel: Controls verbosity of mod_rewrite log
#                  0=off, 9=max
RewriteLogLevel 2

As a side note and imo, I would do this in the config file and not in an .htaccess file because this rule should be applied site-wide on this host/vhost. 作为旁注和imo,我将在配置文件而不是.htaccess文件中执行此操作,因为此规则应在此主机/ vhost的站点范围内应用。 This also makes it impossible for Apache to avoid executing the rewrite by ignoring the .htaccess file. 这也使Apache无法通过忽略.htaccess文件来避免执行重写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM