通用htaccess将www重定向到非www

时间:2008-10-24 18:19:39

标签: apache mod-rewrite url-rewriting

我想将www.example.com重定向到example.com。以下htaccess代码实现了这一点:

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

但是,有没有办法以通用方式执行此操作而不对域名进行硬编码?

24 个答案:

答案 0 :(得分:841)

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

Michael's相同,但这个有效:P

答案 1 :(得分:105)

但是如果我们需要为单独的http和https执行此操作:

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

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

答案 2 :(得分:70)

非www 重定向至 www (均为:http + https)

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

答案 3 :(得分:44)

如果你想在httpd.conf文件中这样做,你可以在没有mod_rewrite的情况下这样做(显然它对性能更好)。

<VirtualHost *>
  ServerName www.example.com
  Redirect 301 / http://example.com/
</VirtualHost>

我在这里得到了答案:https://serverfault.com/questions/120488/redirect-url-within-apache-virtualhost/120507#120507

答案 4 :(得分:33)

以下是将www URL重定向到no-www的规则:

#########################
# redirect www to no-www
#########################

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

以下是将无www网址重定向到www:

的规则
#########################
# redirect no-www to www
#########################

RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]
RewriteRule ^(.*) http://www.%1/$1 [R=301,NE,L]

请注意,我使用NE标志来阻止apache转义查询字符串。如果没有此标记,apache会将请求的网址http://www.example.com/?foo%20bar更改为http://www.example.com/?foo%2250bar

答案 5 :(得分:11)

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^/(.*)$ http://%1/$1 [R]

RewriteCond在<{em> HTTP_HOST之后抓取www.变量中的所有内容,并将其保存在%1中。

RewriteRule捕获不带前导/的网址,并将其保存在$1

答案 6 :(得分:7)

试试这个:

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

如果主机以www开头,我们会将整个主机粘贴到网址的开头,然后取消“www。”

答案 7 :(得分:6)

我发现,有很多关于htaccess重定向的错误信息。首先,如果您希望此代码能够运行,请确保您的站点在使用Apache的Unix上运行,而不是在Windows主机上运行。

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]

RewriteRule ^(.*)$ http://%1/$1 [R=301,L] 

(确保每行文本之间没有行间距;我在行之间添加了额外的空格,以便在此窗口中呈现正常。)

这是一段代码,可用于将您网站的www版本指向http://版本。还可以使用其他类似的代码。

答案 8 :(得分:6)

完整的通用WWW处理程序,http / https

我没有看到完整的答案。我使用它来处理WWW包含。

  1. 通用。不需要域信息。
  2. 在主域名上强行WWW:www.domain.com
  3. 删除子域名上的WWW:sub.domain.com
  4. 保留HTTP / HTTPS状态。
  5. 允许域/子域的个人Cookie
  6. 请让我知道这是如何运作的,或者我是否留下了漏洞。

    RewriteEngine On
    RewriteBase /
    
    # Force WWW. when no subdomain in host
    RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$ [NC]
    RewriteCond %{HTTPS}s ^on(s)|off [NC]
    RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
    # Remove WWW. when subdomain(s) in host     
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteCond %{HTTPS}s ^on(s)|off [NC]
    RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)(.+\.)(.+\.)(.+)$ [NC]
    RewriteRule ^ %1%3%4%5%{REQUEST_URI} [R=301,L]
    

答案 9 :(得分:3)

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

对于子文件夹

答案 10 :(得分:3)

www到非www与https

RewriteEngine on

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

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

答案 11 :(得分:3)

对于那些需要能够访问整个网站的人没有'www'前缀。

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]

请务必将此添加到以下文件

/site/location/.htaccess 

答案 12 :(得分:2)

RewriteEngine on
# if host value starts with "www."
RewriteCond %{HTTP_HOST} ^www\.
# redirect the request to "non-www"
RewriteRule ^ http://example.com%{REQUEST_URI} [NE,L,R]

如果您要删除wwwhttp上的https,请使用以下内容:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS}s ^on(s)|offs
RewriteRule ^ http%1://example.com%{REQUEST_URI} [NE,L,R]

重定向 非ssl

和 SSL

在apache 2.4.*上,您可以使用带Redirect指令的if来完成此操作,

<if "%{HTTP_HOST} =='www.example.com'">
Redirect / http://example.com/
</if>

答案 13 :(得分:2)

使用:Javascript / jQuery

// similar behavior as an HTTP redirect
window.location.replace("http://www.stackoverflow.com");
// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";

或.htaccess:

RewriteEngine On
RewriteBase /
Rewritecond %{HTTP_HOST} ^www\.yoursite\.com$ [NC]
RewriteRule ^(.*)$ https://yoursite.com/$1 [R=301,L]

和PHP方法:

$protocol = (@$_SERVER["HTTPS"]    == "on") ? "https://" : "http://";

if (substr($_SERVER['HTTP_HOST'], 0, 4) !== 'www.') {
    header('Location: '.$protocol.'www.'.$_SERVER    ['HTTP_HOST'].'/'.$_SERVER['REQUEST_URI']);
    exit;
}

的Ajax

$.ajax({
    type: "POST",
    url: reqUrl,
    data: reqBody,
    dataType: "json",
    success: function(data, textStatus) {
        if (data.redirect) {
            // data.redirect contains the string URL to redirect to
            window.location.href = data.redirect;
        }
        else {
            // data.form contains the HTML for the replacement form
            $("#myform").replaceWith(data.form);
        }
    }
});

答案 14 :(得分:2)

我使用上面的规则将www转到没有www并且它适用于主页,但是在内部页面上它们转发到/index.php

我在.htaccess文件中发现了另一个规则,导致了这个但不知道如何处理它。任何建议都会很棒:

############################################
## always send 404 on missing files in these folders

    RewriteCond %{REQUEST_URI} !^/(media|skin|js)/

############################################
## never rewrite for existing files, directories and links

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l

############################################
## rewrite everything else to index.php

    RewriteRule .* index.php [L]

答案 15 :(得分:1)

使用.htaccess重定向到www或非www:

只需将以下代码行放入主根.htaccess文件中。 在两种情况下,只需将domain.com更改为您自己的主机名即可。

重定向到www

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{HTTP_HOST} ^domain\.com [NC]
  RewriteRule ^(.*)$ http://wwwDOTdomainDOtcom/$1 [L,R=301]
</IfModule>

重定向到非www

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
  RewriteRule ^(.*)$ http://domainDOTcom/$1 [L,R=301]
</IfModule>

将DOT更改为=>。 !!!

答案 16 :(得分:1)

如果.htaccess自定义不是理想选择,则为替代方法:

我已经创建了供公众使用的简单重定向服务器。只需添加A或CNAME记录:

CNAME   r.simpleredirect.net
A       89.221.218.22

更多信息:https://simpleredirect.net

答案 17 :(得分:1)

选择的答案和许多其他解决方案在/之后删除了网址的部分,所以基本上它总是重定向到主域,至少对我而言..所以我在斜线后添加了关于完整路径的工作示例.. < / p>

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

答案 18 :(得分:1)

如果你强迫www。在url或强制ssl协议中,然后尝试使用htaccess文件中的可能变体,例如:

RewriteEngine On
RewriteBase /

### Force WWW ###

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

## Force SSL ###

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://example.com/$1 [R,L]

## Block  IP's ###
Order Deny,Allow
Deny from 256.251.0.139
Deny from 199.127.0.259

答案 19 :(得分:1)

我开始工作的唯一方法......

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

答案 20 :(得分:0)

这已更新为适用于Apache 2.4:

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

Michael's的唯一更改是删除[NC],这会产生&#34; AH00665&#34;错误:

  

非正则表达式的NoCase选项&#39; -f&#39;不受支持且将被忽略

答案 21 :(得分:0)

我不确定你为什么要删除www。 但反向版本将是:

# non-www.* -> www.*, if subdomain exist, wont work
RewriteCond %{HTTP_HOST} ^whattimein\.com
RewriteRule ^(.*)$ http://www.whattimein.com/$1 [R=permanent,L]

此脚本的优点是: 如果你有像test.whattimein.com或任何其他(开发/测试的环境)的东西 它不会将U重定向到原来的环境。

答案 22 :(得分:0)

如果是localhost,则忽略重定向(出于本地环境中的开发目的)而添加。如果不是localhost AND(不是https或www),则重定向到https和非www。

RewriteEngine On

RewriteBase /
RewriteCond %{HTTP_HOST} !localhost [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

答案 23 :(得分:-2)

您好,您可以在htaccess文件中使用以下规则:

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