首页 > 解决方案 > 如何使用带有 $_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 会显示。

谢谢您的合作。

标签: phpapachefriendly-url

解决方案


抱歉,我用葡萄牙语回答的最后一条消息,@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]

所以这行得通。

谢谢您的合作。


推荐阅读