简体   繁体   English

.htaccess重定向到https和www。 如果启用

[英].htaccess redirect to https and www. if enabled

I want to configure my .htaccess file in a manner where it will do these two things: 我想以一种可以执行以下两项操作的方式配置.htaccess文件:

  • ALWAYS make sure www. 务必确保www. is appended (even for subdomains like so: www.sub.example.com ). 被附加(即使对于子域名也是如此: www.sub.example.com )。
  • OPTIONALLY add https:// if it is enabled. 可选地添加https://如果已启用)。

I preferably (if possible) want to do this in one rewrite rule, currently I have this: 我最好(如果可能)希望按照一个重写规则来执行此操作,目前我有:

RewriteEngine On

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

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

So how do I make the s of https:// optional in the same rewrite? 那么,如何让shttps://可选在同一重写?

You can use the following Rule to redirect https , non-https and non-www requests to www with a single redirect. 您可以使用以下规则通过一次重定向将httpsnon-httpsnon-www请求重定向到www

RewriteEngine on


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

This is doing a 302 Temporary redirect. 这是在执行302临时重定向。 If you want a 301 permanent redirect, change the R flag to R=301 . 如果要使用301永久重定向,请将R标志更改为R=301

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM