apache - URL 后面的双斜杠

标签 apache .htaccess ssl url-rewriting apache2

我将任何非 www 和非 SSL 连接转发到 https://www.*,并在我的 .htaccess 中使用此代码:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.tld/$1 [R=301,L]

但是当我尝试将这个确切的代码放入我的 000-default.conf 和 000-default-le-ssl.conf 时,它会放置一个双斜杠 (https://www.domain.tld**//* *) 在 URL 后面。为什么?

最佳答案

But when I try to put this exact code in my 000-default.conf and 000-default-le-ssl.conf, it puts a double slash (https://www.domain.tld//) behind the URL. Why?

这是因为在 http 服务器配置中,前导斜杠在 RewriteRule 中也与 .htaccess 文件不同。

您可以使用此规则使其在两种类型的文件中表现相同:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^/?(.*)$ https://www.domain.tld/$1 [R=301,NE,L]

否则:

RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.domain.tld%{REQUEST_URI} [R=301,NE,L]

关于apache - URL 后面的双斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46604130/

相关文章:

java - Apache hadoop map reduce task 卡住

php - 本地主机上的 mysql_connect()

ssl - X-Frame-Option 问题

javascript - CSRF 基础知识与 SSL 和 CSRF 关系

iOS 推送通知 SSL 证书

php - apache、php、mysql 与 care2x 兼容

java - 无法使用 Apache POI 读取 .doc 文件的整个单元格

apache - 我需要在 .htaccess 文件中重复 RewriteCond 吗?

apache - htaccess 将所有请求重定向到index.php(子文件夹除外)

php - Htaccess 仅用于友好的 url