通用非www到www和非HTTP到https

编程入门 行业动态 更新时间:2024-10-28 16:25:43
本文介绍了通用非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 should redirect to www.domain RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} ^www\.%{HTTP_HOST} [NC] RewriteRule ^(.*)$ www\.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] # All secure (HTTPS) traffic should redirect to www.domain RewriteCond %{HTTPS} on RewriteCond %{HTTP_HOST} !^www\.%{HTTP_HOST} [NC] RewriteRule ^(.*)$ 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/.htaccess~~V或读我需要涵盖在拒绝所有之类的话?

Another point is, can .htaccess be read if i were to go to www.domain/.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 ( 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 should redirect to www.domain RewriteCond %{HTTPS} off RewriteRule ^ www\.%{ENV:MY_TLD}%{REQUEST_URI} [L,R=301] # All secure (HTTPS) traffic should redirect to www.domain 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 ^ www\.%{ENV:MY_TLD}%{REQUEST_URI} [L,R=301]

更多推荐

通用非www到www和非HTTP到https

本文发布于:2023-11-01 19:36:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550221.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:www   HTTP   https

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!