Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache 将根目录重定向到子目录时,如何解释(缺少)尾部斜杠?_Apache_.htaccess_Url Rewriting_Apache2 - Fatal编程技术网

Apache 将根目录重定向到子目录时,如何解释(缺少)尾部斜杠?

Apache 将根目录重定向到子目录时,如何解释(缺少)尾部斜杠?,apache,.htaccess,url-rewriting,apache2,Apache,.htaccess,Url Rewriting,Apache2,在我的共享Apache主机上,我正在尝试将所有到我的网站的请求无缝重定向(无需在客户端浏览器中重写URL)到名为/blog的子目录。我正在使用.htaccess文件执行此操作 这是我到目前为止所做的: 这适用于带有尾部斜杠的URL 例如,当用户访问https://example.com/about/,来自http://example.com/blog/about/已送达,URL仍为https://example.com/about/ 问题 当用户访问https://example.com/abo

在我的共享Apache主机上,我正在尝试将所有到我的网站的请求无缝重定向(无需在客户端浏览器中重写URL)到名为
/blog
的子目录。我正在使用
.htaccess
文件执行此操作

这是我到目前为止所做的:

这适用于带有尾部斜杠的URL

例如,当用户访问
https://example.com/about/
,来自
http://example.com/blog/about/
已送达,URL仍为
https://example.com/about/

问题

当用户访问
https://example.com/about
(无尾随斜杠),来自
https://example.com/blog/about/
已提供,但浏览器中的URL更改为
https://example.com/blog/about/
。不需要更改URL

问题

如何更改
.htaccess
文件,以确保在缺少尾部斜杠时,客户端浏览器中的URL不会更改

其他信息


我在我的提供者的后端签入的唯一选项是“强制SSL”。服务器上没有其他
.htaccess
文件。

Apache的
mod_dir
模块正在添加尾随斜杠,因为
/blog/about/
是一个真实目录,并且在目录前面添加尾随斜杠以防止目录列出

您可以使用以下规则:

DirectorySlash Off
Options -Indexes
RewriteEngine On

# add a trailing slash if desrtination is a directory
RewriteCond %{DOCUMENT_ROOT}/blog%{REQUEST_URI} -d
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]

RewriteCond %{HTTP_HOST} ^example\. [NC]
RewriteRule !^blog/ blog%{REQUEST_URI} [NC,L]

请确保在新浏览器中对此进行测试,以避免旧缓存。

感谢您的回复,但使用您的代码,问题仍然存在(使用新浏览器进行测试)。当我输入
https://example.com/about
它仍然将URL重写为
https://example.com/blog/about/
。这些规则在我的Apache上起作用,因此我怀疑您的另一个冲突规则。你能在你的问题中展示你的完整的.htaccess吗?同时告诉我你的.htaccess在哪里?我也担心会有一些共享托管魔法发生。我发布的是完整的.htaccess文件。它位于我的Web空间的根文件夹中。感谢您继续提供建议,但我将减少损失,今天到此为止(我已经努力让它工作了两天)。我会接受你的答案,因为它在你的盒子上工作-我很确定在我这边有一些共享主机的奇怪之处。以后也可以随时问与这个问题相关的问题,祝你好运。
DirectorySlash Off
Options -Indexes
RewriteEngine On

# add a trailing slash if desrtination is a directory
RewriteCond %{DOCUMENT_ROOT}/blog%{REQUEST_URI} -d
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]

RewriteCond %{HTTP_HOST} ^example\. [NC]
RewriteRule !^blog/ blog%{REQUEST_URI} [NC,L]