php - 使用路由将 htaccess 重写为 https

标签 php .htaccess mod-rewrite https

我为我的网站使用以下 .htaccess。如果它们不是位于 url 的文件,我将所有 url 重定向到 index.php。

Options +FollowSymLinks
IndexIgnore */*
# Turn on the RewriteEngine
RewriteEngine On
#  Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

这边http://website.com/user/user_name/转到 index.php 和 http://website.com/css/style.css给出实际的 css 文件(如果 style.css 存在)。

这很好用,只是我想在 url 上强制使用 https://,但我无法让它工作。任何帮助是极大的赞赏。

编辑:

使用 Jeroen 的回答我得到了它与以下 .htaccess 一起工作

Options +FollowSymLinks
IndexIgnore */*
# Turn on the RewriteEngine
RewriteEngine On
#  Rules
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

最佳答案

当您使用 CloudFlare 的 ssl - 我假设是 Flexible SSL - 尽管@Fox 的解决方案对于“正常”ssl 连接是正确的,但您需要一个不同的解决方案。

对于 CloudFlare,您需要这样的东西:

RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L,QSA]

另见 CloudFlare's support article .

关于php - 使用路由将 htaccess 重写为 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26406001/

相关文章:

php - Mysql数据库连接到PHP

.htaccess:将所有请求重定向到子目录,除非存在确切的目录

linux - .htaccess 文件破坏了我的 WP 网站

nginx - Nginx 中的多站点 WordPress 重写规则

asp.net - web.config 下用于 url 重写的多个外部文件

php - 用 PHP 改变 .htaccess - 删除重写规则

php - HTML 显示文章

html - .htaccess - 隐藏包含文件的文件夹

Apache 在使用 http 时显示默认页面而不是 https

php - 如何在 Doctrine 2 中获取类而不是数组