斜线导致 404 错误和不正确的 url 行为:APACHE/ PHP

Trail slash is causing 404 error and improper url behavior: APACHE/ PHP

希望你们中的一位能提供帮助。我正在使用 php 进行开发,没有框架,因此我所有的路由规则都在 .htaccess 中,而不是 .php

这是我的问题。假设我的网站是 http://example.com and my navigation is pretty straight forward, meaning if you click the 'press' page link you will go to http://example.com/press or http://example.com/press/,两者都很好。

但是,如果你碰巧去 http://example.com/press/ 点击我导航中的另一个内部 link,例如 'about' 等。我不会带你去 '/example ' 而是显示 404 的 '/press/example',因为没有页面或目录来处理此问题。

我在我的 .htaccess 中尝试了 'DirectorySlash Off',但是如果 url 末尾有 /,所有这些页面都无法显示。

我有一个非常基本的 .htaccess 设置,见下文:

<IfModule mod_rewrite.c>

RewriteEngine On
# Options -Indexes
# GEN CONFIG



# Handle Errors
ErrorDocument 403 /views/errors/404.php
ErrorDocument 404 /views/errors/404.php
ErrorDocument 303 /views/errors/404.php

# Url renamin
RewriteRule ^(about-us|About-Us|ABOUT-US)/?$ views/about-us.php [L]

RewriteRule ^(press|Press|PRESS)/?$ views/press.php [L]
RewriteRule ^(faq|Faq|FAQ)/?$ views/faq.php [L]

</IfModule>

go to http://example.com/press/ click another internal link in my nav, such as about etc.. I will not take you to /about but rather /press/about

这个问题不是因为重写规则,而是因为你使用了相对路径。

要解决此问题,您可以将其添加到页面 HTML:

<head> 部分正下方
<base href="/" />

这样每个 相关 URL 都是从该基础 URL 解析的,而不是从当前页面的 URL.