php - 如何使用带有 $_SERVER['REQUEST_URI'] 的友好 url 强制 https?
问题描述
伙计们!
我有一个在 localhost 的 Apache2 上运行的 PHP 应用程序。系统使用来自 $_SERVER['REQUEST_URI'] 的友好 URL。我的.htaccess:
RewriteEngine On
#RewriteRule ^(.*)$ https://myapp.localhost/$1 [R=permanent]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
RewriteRule ^$ index.php?/ [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?/$1 [QSA,L]
如果我注释第 3 行和第 4 行,系统运行正常。如果我使用我向您展示的代码,他会将 URL 写为https://myapp.localhost/index.php?/它需要写https://myapp.localhost/。SSL 认证没问题。
使用此配置无法找到图像和档案,但没有第 3 行和第 4 行并且手动使用 https 会显示。
谢谢您的合作。
解决方案
抱歉,我用葡萄牙语回答的最后一条消息,@Martin 先生。
也许问题出在使用多个教程。经过这么多的搜索。我明白:有必要删除 /etc/apache2/sites-available/bigpecasnew-localhost-ssl.conf 中的一些代码行,并在https://www.digitalocean.com/community/tutorials<Directory /home/user/site>SSLOptions +StdEnvVars</Directory>
中阅读时
添加这些行/how-to-create-a-self-signed-ssl-certificate-for-apache-in-ubuntu-18-04。
在.htaccess 上更改我的代码后:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
RewriteRule ^$ index.php?/ [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?/$1 [QSA,L]
所以这行得通。
谢谢您的合作。
推荐阅读
- python - 如何在 Python 3 中对随机数进行迭代?
- oracle - 授予角色权限
- c++ - 在简单的 Point 类中,getter/setter 是否有任何真正的论据,而不是公共成员变量?
- c# - C# 7.0 – Discards 和 IDisposable out 参数
- velo - 单击 wix 代码时将按钮增加 1
- c# - 接收 UDP 数据包并将其保存在字符串数组中
- java - 我不明白单例类的概念
- npm - npm 无法更改 Jasmine 包版本
- multithreading - 如何在 OCaml 中使用线程模块
- avfoundation - 您如何实际播放 AVAssetTrack,而不是 AVURLAsset?