通用非www到www和非HTTP到https [英] generic non-www to www, and non-http to https

查看:271
本文介绍了通用非www到www和非HTTP到https的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的$ C $下,我已经拿起从这里,并试图适应它,因为我来自的.htaccess明白我的.htaccess文件,但我似乎无法得到它的工作(或者在浏览器缓存,但我似乎无法将其清除)。

I have the following code for my .htaccess file that I've picked up from here and tried adapting it as I understand from .htaccess, yet I can't seem to get it to work (or maybe the browser has cached it but I can't seem to clear it).

Options -Indexes
Options +FollowSymlinks

<IfModule mod_rewrite.c>

########## FORCE SSL ##########
RewriteEngine On
RewriteBase / 

# Non-secure requests to www.domain.com should redirect to https://www.domain.com
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.%{HTTP_HOST} [NC]
RewriteRule ^(.*)$ https://www\.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

# All secure (HTTPS) traffic should redirect to https://www.domain.com
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.%{HTTP_HOST} [NC]
RewriteRule ^(.*)$ https://www\.%{HTTP_HOST}/$1 [L,R=301] 

</IfModule>

我希望把它作为一般的可能,所以我可以简单地复制并粘贴到任何网站我做出这样就无需每次编辑它 - 我想我也可以在PHP中做到这一点,但我认为这将是一件好事如果htaccess的也有。

I want to make it as generic a possible so I can simply copy and paste it to any site I make so no need to edit it each time - I guess I could also do this in PHP but I think it would be good if .htaccess is also there.

还有一点就是,可以的.htaccess如果我是去www.domain.com/.htaccess~~V或读我需要涵盖在拒绝所有之类的话?

Another point is, can .htaccess be read if i were to go to www.domain.com/.htaccess or do I need to cover that in a 'deny all' kind of thing?

推荐答案

的第二个参数的的RewriteCond 的必须是正则表达式,因此它不能包含变量

The second argument to the RewriteCond must be a regex, so it cannot contain a variable.

尝试添加以下到你的.htaccess文件来代替你有规则

Try adding the following to your .htaccess file in place of the rules you had

#capture top level domain (.com or .co.uk)
RewriteCond %{HTTP_HOST} ([-_a-zA-Z0-9]+\.([a-zA-Z]{2,5}|co\.uk))$ [NC]
RewriteCond %{HTTP_HOST} (www\.)?(.+)$ [NC]
RewriteRule ^ - [E=MY_TLD:%2]

# Non-secure requests to www.domain.com should redirect to https://www.domain.com
RewriteCond %{HTTPS} off
RewriteRule ^ https://www\.%{ENV:MY_TLD}%{REQUEST_URI} [L,R=301] 

# All secure (HTTPS) traffic should redirect to https://www.domain.com
RewriteCond %{HTTPS} on
#if host does not start with www
RewriteCond %{HTTP_HOST} !^www\.[-_a-zA-Z0-9]+\.([a-zA-Z]{2,5}|co\.uk)$ [NC]
RewriteRule ^ https://www\.%{ENV:MY_TLD}%{REQUEST_URI} [L,R=301] 

这篇关于通用非www到www和非HTTP到https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆