首页 > 解决方案 > .htaccess 用于排除 URL 并删除 www。并切换到 https

问题描述

我正在尝试使用 .htaccess 编写重定向/重写规则,以满足这些需求:

在这里,我有点坚持我的知识:

Options +FollowSymLinks 
IndexIgnore */*
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301] 

RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/api/do/ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

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

# if a directory or a file exists, use it directly 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php 
RewriteRule ^.*$ /index.php [L]

以下是一些示例 URL(我希望这能说明问题):

http://www.test.de --> https://test.de
http://www.test.de/ --> https://test.de
https://www.test.de/ --> https://test.de
http://test.de/api/do/ --> http://test.de/api/do/

标签: .htaccess

解决方案


我想这就是你需要的

   RewriteEngine on
   RewriteCond %{HTTPS} !=on
   RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
   RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
   # CONFIGURE HTTPS
   RewriteCond %{SERVER_PORT} 80
   RewriteRule ^(.*)$ https://YOUR-SITE.COM$1 [R,L]

https://YOUR-SITE.COM替换为您的当前域


推荐阅读