.htaccess - .htaccess 用于排除 URL 并删除 www。并切换到 https
问题描述
我正在尝试使用 .htaccess 编写重定向/重写规则,以满足这些需求:
- 从 URL 中删除 www
- 将 http 切换为 https
- 删除尾部斜杠
- 从 https 中排除一个 URL
在这里,我有点坚持我的知识:
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/
解决方案
我想这就是你需要的
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替换为您的当前域
推荐阅读
- python - 如何将列表变成方阵?
- powershell - 用于删除 AD 用户和配置文件的 Powershell 脚本
- javascript - Zendesk Live chat 与 React Web 应用程序集成
- javascript - 如何使用 d3 在 bar 中呈现重复的图像?
- javascript - React Redux 无法在该字段中键入值时更新输入字段
- python - 获得第一个输入后如何让我的代码循环到输入?
- amazon-msk - 配置与 MSK 集群通信的 SQL CDC 实例时出错
- javascript - 使用 HTTP (NodeJS) 执行带有正文参数的函数
- c - 如何在继续程序之前先检查输入?还是不应该?为什么?
- javascript - Array.map 中的 React 功能组件在将函数作为道具传递时总是重新渲染