簡體   English   中英

我想將所有http請求重定向到https,並將所有www請求重定向到非www

[英]I want to redirect all http requests to https, and all www requests to non-www

這是我當前的.htaccess文件:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php
</IfModule>

我想將所有http請求重定向到https,並將所有www請求重定向到非www,並將所有文件請求重定向到index.php

例如:

http://www.example.comhttps://example.com

https://www.example.comhttps://example.com

http://example.com/file.php訪問https://example.com/index.php

一切似乎都在工作,除了www部分..有什么幫助嗎?

http://www.example.comhttps://example.com
https://www.example.comhttps://example.com
http://example.com/file.php訪問https://example.com/index.php

也許這會奏效:

RewriteEngine On

# Remove www from https requests. 
# Next 3 lines must be placed in one .htaccess file at SSL root folder, 
# if different from non-ssl.
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST}  ^www\.(.+) [NC]
RewriteRule ^(.*) https://%1/$1 [R=301,L]

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

如果不起作用,請嘗試更換

RewriteCond %{HTTPS} offon

RewriteCond %{HTTP:X-Forwarded-SSL} offon RewriteCond %{HTTP:X-Forwarded-SSL} off

您可以添加一個處理www部分的附加規則

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

RewriteCond捕獲www.之后的所有內容www. 並在RewriteRule中將其用作%1

當一切按預期工作時,您可以將R更改為R=301

從不測試啟用301 ,請參閱此答案有關調試.htaccess重寫規則的提示以獲取詳細信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM