通用htaccessredirectwww到非www
我想将www.example.comredirect到example.com 。 以下htaccess代码使这发生了: 
 RewriteCond %{HTTP_HOST} ^www\.example\.com [NC] RewriteRule ^(.*)$ http://example.com/$1 [L,R=301] 
但是,有没有办法以通用的方式做这个没有硬编码的域名?
 RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*)$ http://%1/$1 [R=301,L] 
和迈克尔一样,除了这个作品:P
但是,如果我们需要为单独的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] 
redirect非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] 
如果你想在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
以下是将www URLredirect到no-www的规则:
 ######################### # redirect www to no-www ######################### RewriteCond %{HTTP_HOST} ^www\.(.+) [NC] RewriteRule ^(.*) http://%1/$1 [R=301,NE,L] 
以下是将no-www URLredirect到www的规则:
 ######################### # redirect no-www to www ######################### RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC] RewriteRule ^(.*) http://www.%1/$1 [R=301,NE,L] 
 请注意,我使用NE标志来防止Apache转义查询string。 如果没有这个标志,apache会把请求的URL改成http://www.example.com/?foo%20bar到http://www.example.com/?foo%2250bar 
 RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^/(.*)$ http://%1/$1 [R] 
RewriteCond在“www” 之后捕获HTTP_HOSTvariables中的所有内容。 并将其保存在%1。 RewriteRule捕获URL(不含前导“/”)并将其保存在$ 1中。
尝试这个:
 RewriteCond %{HTTP_HOST} ^www\. [NC] RewriteRule ^(.*)$ %{HTTP_HOST}$1 [C] RewriteRule ^www\.(.*)$ http://$1 [L,R=301] 
如果主机以www开始,我们将整个主机粘贴到URL的开头,然后摘下“www”。
我发现可能有很多关于htaccessredirect的错误信息。 首先,确保您的站点使用Apache在Unix上运行,而不是在Windows主机上运行,如果您希望此代码正常工作。
 RewriteEngine On RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteRule ^(.*)$ http://%1/$1 [R=301,L] 
(确保每行文本之间没有空格,但是在行之间添加了额外的空格,因此在此窗口中显示为空。
这是可以用来将您的网站的www版本指向http://版本的一段代码。 还有其他类似的代码也可以使用。
完整的通用WWW处理程序,http / https
我没有看到完整的答案。 我用这个来处理WWW包含。
- 通用。 不需要域信息。
- 在主域上强制WWW:www.domain.com
- 删除子域上的WWW:sub.domain.com
- 保留HTTP / HTTPS状态。
- 允许单个的域名/子域名的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] 
 RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*)$ http://%1/subfolder/$1 [R=301,L] 
对于子文件夹
对于那些需要能够访问整个网站没有 “www”前缀。
 RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301] 
马雷确定你添加到以下文件
 /site/location/.htaccess 
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] 
我不知道你为什么要删除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或任何其他(开发/testing环境)的东西,它不会redirect到原来的环境。
我使用上面的规则转发www到没有www,它的主页工作正常,但是在内部页面,他们转发到/index.php
我在我的.htaccess文件中find了这个其他规则,但是不知道该怎么办。 任何build议将是伟大的:
 ############################################ ## 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] 
唯一的办法,我得到它的工作…
 RewriteEngine On RewriteCond %{HTTP_HOST} ^site\.ro RewriteRule (.*) http://www.site.ro/$1 [R=301,L] 
如果你迫使www。 在URL或强制ssl prototcol,然后尝试在htaccess文件中使用可能的变化,如:
 RewriteEngine On
 RewriteBase /
 ###强制WWW ###
 RewriteCond%{HTTP_HOST} ^示例\ .com
 RewriteRule(。*)http://www.example.com/$1 [R = 301,L]
 ##强制SSL ###
 RewriteCond%{SERVER_PORT} 80 
 RewriteRule ^(。*)$ https://example.com/$1 [R,L]
 ##阻止IP的###
拒绝订单,允许
拒绝从256.251.0.139
拒绝从199.127.0.259
 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] 
 如果您想要在http和https上删除www ,请使用以下命令: 
 RewriteEngine on RewriteCond %{HTTP_HOST} ^www\. RewriteCond %{HTTPS}s ^on(s)|offs RewriteRule ^ http%1://example.com%{REQUEST_URI} [NE,L,R] 
这将redirectNon ssl
至
和SSL
至
 在Apache 2.4.*你可以完成这个使用if指令Redirect , 
 <if "%{HTTP_HOST} =='www.example.com'"> Redirect / http://example.com/ </if> 
这是更新到Apache 2.4的工作:
 RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^www\.(.*)$ RewriteRule ^(.*)$ http://%1/$1 [R=301,L] 
 与迈克尔唯一的变化是删除产生“AH00665”错误的[NC] : 
非正则expression式模式'-f'的NoCase选项不受支持,将被忽略
所选答案和许多其他的解决scheme在这里下降后的url的部分,所以基本上它总是redirect到主域,至less对我来说..所以我join工作示例尊重全path后斜线..
 RewriteEngine On RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [L,R=301] 
使用: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); } } }); 
嗨,你可以在你的htaccess文件中使用以下规则:
 RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com RewriteRule (.*) http://www.example.com/$1 [R=301,L]