【问题标题】:apache redirect http to https without wwwapache 将 http 重定向到 https 而不使用 www
【发布时间】:2017-06-07 05:38:50
【问题描述】:

尝试将所有 http 和 none-www 流量重定向到 https(带或不带 www)。 使用.htaccessapache VirtualHost 文件:

虚拟主机文件

<VirtualHost *:80>
    ServerName adi-md.co.il
    ServerAlias www.adi-md.co.il

.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTP_HOST} ^adi-md.co.il
#RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L,NE]
#RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
#RewriteRule (.*) https://www.%1%{REQUEST_URI} [L,R=301]
#RewriteCond %{HTTP_HOST} ^www.adi-md.co.il

选项 1

RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^adi-md.co.il

选项 2

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

仅输入adi-md.co.il 时,您将获得服务器的默认页面。

如何解决它,以便所有流量都重定向到 https 以及 none-www 请求??

注意: 我看过这 3 个,但它们没有提供答案(2 out 3 处理 www 到 none-www)

apache redirect http to https and www to non www
http to https apache redirection
Redirect from http to https without www using .htaccess

【问题讨论】:

标签: php apache .htaccess redirect mod-rewrite


【解决方案1】:

不带 www 将 http 重定向到 https 的两个简单规则:

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

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

【讨论】:

  • 是否可以使用这些规则。但是在使用 .local 域名时忽略它们?
【解决方案2】:

试试这个:

RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} =on
RewriteRule ^ http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L,NE]

这基本上首先检查您是否启用了www 并打开了HTTPs。如果有,它会将其切换回HTTP 并删除www

确保在测试之前清除缓存。

【讨论】:

  • 不知何故这没有做这项工作...我更新了我的问题.htaccess 文件
猜你喜欢
  • 2012-04-14
  • 2017-02-02
  • 2014-12-13
  • 2015-02-16
  • 2019-09-05
  • 1970-01-01
  • 2016-03-27
  • 2016-01-20
  • 1970-01-01
相关资源
最近更新 更多