使用 .htaccess 从 http 重定向到 https 而无需 www
Redirect from http to https without www using .htaccess
我有基于 PHP 和 Apache 的应用程序。如果请求 link 不是目录或文件,则所有请求都转到 index.php 文件。我想将所有请求从 http 重定向到 https 并且没有 www.
对我有效link:
从关注 links 开始对我来说无效(抱歉,我的声誉很小,我不能 post 更多 2 links):
- http://
- http://www.
- www.
我的 htaccess 看起来像那样
AddDefaultCharset UTF-8
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php [L]
如何在没有 www 的情况下重定向到 https?
还有一条 http->http
和 www
移除规则:
AddDefaultCharset UTF-8
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://someaddress.org%{REQUEST_URI} [L,NE,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
您可以使用 SSLRequireSSL 指令覆盖 http 到 https 部分。
<Directory />
SSLRequireSSL
</Directory>
如果问题出在 htaccess 文件而不是重写规则,您可以停止使用 .htaccess 文件并将重写规则移至 apache 配置文件。
如果您只是不想或不能使用重写规则,并且由于绝大多数 php 应用程序需要在每个脚本中包含一个配置文件(例如配置变量或用户控件),您可以使用这个文件以获取请求的资源并去除 www.部分如果域名以它们开头。
如果包含文件可能是您需要的解决方案,我可以提供 php 代码示例。
首先将所有有WWW的请求重定向到没有WWW的https
# For http://www.yourdomain.com and https://www.yourdomain.com
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com [NC]
RewriteRule ^(.*)$ https://yourdomain.com/ [L,R=301]
现在将所有没有 WWW 的 http 请求重定向到没有 WWWW 的 https
# For http://yourdomain.com
RewriteCond %{HTTPS} =off
RewriteCond %{HTTP_HOST} ^yourdomain\.com [NC]
RewriteRule ^(.*)$ https://yourdomain.com/ [L,R=301]
在 .htacess 文件中依次添加以上代码,将您的所有请求重定向到没有 www 的 https
我使用 "R=301" 进行永久重定向
我有基于 PHP 和 Apache 的应用程序。如果请求 link 不是目录或文件,则所有请求都转到 index.php 文件。我想将所有请求从 http 重定向到 https 并且没有 www.
对我有效link:
从关注 links 开始对我来说无效(抱歉,我的声誉很小,我不能 post 更多 2 links):
- http://
- http://www.
- www.
我的 htaccess 看起来像那样
AddDefaultCharset UTF-8
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php [L]
如何在没有 www 的情况下重定向到 https?
还有一条 http->http
和 www
移除规则:
AddDefaultCharset UTF-8
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://someaddress.org%{REQUEST_URI} [L,NE,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
您可以使用 SSLRequireSSL 指令覆盖 http 到 https 部分。
<Directory />
SSLRequireSSL
</Directory>
如果问题出在 htaccess 文件而不是重写规则,您可以停止使用 .htaccess 文件并将重写规则移至 apache 配置文件。
如果您只是不想或不能使用重写规则,并且由于绝大多数 php 应用程序需要在每个脚本中包含一个配置文件(例如配置变量或用户控件),您可以使用这个文件以获取请求的资源并去除 www.部分如果域名以它们开头。
如果包含文件可能是您需要的解决方案,我可以提供 php 代码示例。
首先将所有有WWW的请求重定向到没有WWW的https
# For http://www.yourdomain.com and https://www.yourdomain.com
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com [NC]
RewriteRule ^(.*)$ https://yourdomain.com/ [L,R=301]
现在将所有没有 WWW 的 http 请求重定向到没有 WWWW 的 https
# For http://yourdomain.com
RewriteCond %{HTTPS} =off
RewriteCond %{HTTP_HOST} ^yourdomain\.com [NC]
RewriteRule ^(.*)$ https://yourdomain.com/ [L,R=301]
在 .htacess 文件中依次添加以上代码,将您的所有请求重定向到没有 www 的 https
我使用 "R=301" 进行永久重定向