【问题标题】:How to make .htaccess ignore css, js, images and href (links)如何使 .htaccess 忽略 css、js、图像和 href(链接)
【发布时间】:2016-04-11 03:34:57
【问题描述】:

请原谅我的写作,我是 .htaccess 的新手,但我检查了很多示例,但它们都不起作用。

以下是我的文件目录

父/css/style.css
父/js/script.js
父/图像/example.png

我在根目录有一个php文件parent/discussion-forum.php

如果我将它重写为 parent/discussion
RewriteRule ^discussion discussion-forum.php [NC,L]

,除了 href(链接)之外,一切都很好

如果我将其重写为 parent/forum
RewriteRule ^forum discussion-forum.php [NC,L]

,我将面临以下问题
  • parent/css/style.css 变成 parent/forum/css/style.css
  • parent/js/script.js 变成 parent/forum/js/script.css
  • 图片变成parent/forum/image/example.png
  • parent/discussion-forum.php 上其他页面的链接(href)变为 parent/forum/example.php 而不是 parent/example.php
  • 每个名为 forum 的文件,例如 parent/forum-discussion-view.php 都被重定向回 parent/forum

请帮助我解决此问题的 .htaccess 解决方案,在此先感谢。

【问题讨论】:

    标签: php .htaccess


    【解决方案1】:

    我认为您不需要编辑 .htaccess,只需在您的 php 代码中添加一个 $base_href 变量,如下所示:

    $base_href = "http://www.example.com";

    并且在您制作的每个包含中:

    <link href="<?php echo $base_href ?>/css/style.css">
    

    这对我有用。

    【讨论】:

    • 非常感谢您的回复,我以前使用过 css 和 javascript 的绝对路径并且它工作但我需要一个 htaccess 解决方案,以便我可以使用相对路径并且链接仍然被重定向使用绝对路径后返回论坛。
    猜你喜欢
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 2016-07-24
    • 1970-01-01
    • 2016-07-06
    相关资源
    最近更新 更多