525

我想重定向www.example.comexample.com. 以下 htaccess 代码使这种情况发生:

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

但是,有没有办法在不硬编码域名的情况下以通用方式做到这一点?

4

24 回答 24

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

和迈克尔的一样,除了这个有效:P

于 2009-08-13T06:05:20.280 回答
117

但是,如果我们需要对单独的 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]
于 2010-03-19T08:51:48.657 回答
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]
于 2011-03-10T15:42:10.487 回答
46

如果您想在 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

于 2011-03-10T02:31:27.143 回答
33

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

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

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

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

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

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

请注意,我使用NEflag 来防止 apache 转义查询字符串。如果没有这个标志,apache 会将请求的 URL 更改http://www.example.com/?foo%20barhttp://www.example.com/?foo%2250bar

于 2012-04-28T10:21:02.077 回答
13
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^/(.*)$ https://%1/$1 [R]

RewriteCond捕获HTTP_HOST变量的所有内容并将其www.保存在%1.

RewriteRule捕获没有前导的 URL并将/其保存在$1.

于 2008-10-24T20:00:06.590 回答
8

试试这个:

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

如果主机以 www 开头,我们将整个主机粘贴到 URL 的开头,然后去掉“www”。

于 2008-10-24T18:26:48.260 回答
8

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

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

  1. 通用的。不需要域信息。
  2. 在主域上强制使用 WWW:www.domain.com
  3. 删除子域上的 WWW:sub.domain.com
  4. 保留 HTTP/HTTPS 状态。
  5. 允许域/子域的单个 cookie

请让我知道这是如何工作的,或者我是否留下了漏洞。

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]
于 2016-02-09T16:43:06.230 回答
7

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

RewriteEngine On

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

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

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

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

于 2011-01-17T16:06:28.737 回答
4
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/subfolder/$1 [R=301,L]

对于子文件夹

于 2011-10-19T14:36:17.633 回答
3

对于那些需要能够在没有“www”前缀的情况下访问整个站点人。

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

确保将其添加到以下文件中

/site/location/.htaccess 
于 2014-09-04T18:23:19.350 回答
3

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

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

重定向到 www

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

重定向到非 www

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{HTTP_HOST} ^www\.domain\.tld [NC]
  RewriteRule ^(.*)$ http://domain.tld/$1 [L,R=301]
</IfModule>
于 2019-04-18T08:22:22.300 回答
2

我使用上述规则将 www 转发到 no 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]
于 2012-05-22T08:10:11.470 回答
2

使用 https 从 www 到非 www

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]
于 2015-03-03T11:50:52.513 回答
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]

如果要同时删除wwwand httphttps请使用以下命令:

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.*您可以使用Redirectwithif指令完成此操作,

<if "%{HTTP_HOST} =='www.example.com'">
Redirect / http://example.com/
</if>
于 2016-07-10T08:12:07.150 回答
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({
    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);
        }
    }
});
于 2017-08-02T06:37:03.657 回答
1

我让它工作的唯一方法......

RewriteEngine On
RewriteCond %{HTTP_HOST} ^site\.ro
RewriteRule (.*) http://www.site.ro/$1 [R=301,L]
于 2011-02-28T18:21:34.897 回答
1

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

重写引擎开启
RewriteBase /

### 强制万维网###

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

## 强制 SSL ###

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

##阻止IP的###
订单拒绝,允许
拒绝来自 256.251.0.139
拒绝来自 199.127.0.259
于 2014-04-24T15:38:58.040 回答
1

这已更新为适用于 Apache 2.4:

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

与迈克尔的唯一变化是删除[NC],这会产生“AH00665”错误:

不支持非正则表达式模式“-f”的 NoCase 选项,将被忽略

于 2016-05-05T16:07:53.833 回答
1

这里选择的答案和许多其他解决方案在 / 之后删除了 url 的一部分,所以基本上它总是重定向到主域,至少对我来说是这样。所以我在斜杠后添加了关于完整路径的工作示例。

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [L,R=301]
于 2017-01-15T15:17:03.437 回答
1

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

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

CNAME   r.simpleredirect.net
A       89.221.218.22

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

于 2019-10-18T22:11:51.957 回答
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 或任何其他(开发/测试环境)的东西,它不会将你重定向到原始环境。

于 2011-12-16T18:07:11.670 回答
0

添加了如果本地主机,则忽略重定向(用于本地环境中的开发目的)。如果不是 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]
于 2019-12-13T08:54:12.017 回答
-3

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

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
于 2015-05-05T12:29:10.297 回答