htaccess mod 重写 - 我的一半规则(所有类似规则)不起作用



我一直在研究这个问题,希望其他人能插话帮助我。

我正在构建一个新站点(在我们的开发服务器上(,并且在使用一些简单的 SEO 友好 URL 时遇到问题。

我只是将一些SEO友好的关键字重定向到静态PHP页面。 我的文件适用于大多数链接,但 2 个因未知原因而中断。

这是我的文件:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^about-us$ aboutus.php [L]
RewriteRule ^resource-center$ resourcecenter.php [L]
RewriteRule ^our-services$ ourservices.php [L]
RewriteRule ^management-solutions$ ms.php [L]
RewriteRule ^additional-services$ additionalservices.php [L]
RewriteRule ^contact-us$ contactus.php [L]

出于某种原因,资源中心和联系我们将转到 404 页面。 当我单击链接时,正在向我们和资源中心添加一个尾部斜杠,但不会在任何其他链接上添加。

如果我编辑 htaccess 文件并将 ^resource-center$ 更改为 ^resourcecenter$,它将正常工作。 如果我将其更改为 ^resource-center/$ 以考虑添加的尾部斜杠,页面将加载,但不会加载任何 CSS/JS/图像。

我在这里被难住了,特别是因为它看起来如此不一致。 所有文件都存在,没有会干扰的 htaccess 文件,HTML 或 htaccess 中的所有链接都以相同的方式格式化,但其中 2 个每次都会给我错误。

如果有人可以帮助我,将不胜感激。

  • 关闭MultiViews选项
  • 允许可选的尾部斜杠

试试这个代码:

Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteRule ^about-us/?$ aboutus.php [L,NC]
RewriteRule ^resource-center/?$ resourcecenter.php [L,NC]
RewriteRule ^our-services/?$ ourservices.php [L,NC]
RewriteRule ^management-solutions/?$ ms.php [L,NC]
RewriteRule ^additional-services/?$ additionalservices.php [L,NC]
RewriteRule ^contact-us/?$ contactus.php [L,NC]

对于css/js等,您可以在页面的HTML标题中添加以下内容:

<base href="/" />

以便从/而不是从当前页面的 URL 解析每个相对 URL。

最新更新