【问题标题】:Relative paths are not loading when used in html after using RewriteRule in htaccess在 htaccess 中使用 RewriteRule 后,在 html 中使用时不加载相对路径
【发布时间】:2013-06-15 21:03:11
【问题描述】:

我在网上搜索了一个解决方案。

我的 Web 应用程序文件位于 http://localhost.com/exampleFolder/ 我已将 smoe 重写器更新到我的 .htaccess 文件中,如下所示

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-l [OR]
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .?- [S=3]
    RewriteRule ^(.*)/(.*)/(.*)?(.*)$ index.php?aa=$1&bb=$2&cc=$3&%{QUERY_STRING} [L]
    RewriteRule ^(.*)/(.*)?(.*)$ index.php?aa=$1&bb=$2&%{QUERY_STRING} [L]
    RewriteRule ^(.*)?(.*)$ index.php?aa=$1&%{QUERY_STRING} [L]

我已将 html 中的基值更新为

<base href="//localhost/exampleFolder/" />

我将我的 css 链接为

<link rel="stylesheet" type="text/css" href="css/style.css" media="all" />

当我调试代码时,鼠标悬停在我得到的 href 上

localhost/exampleFolder/css/style.css

但仍然没有加载 CSS。 当我使用直接路径时,我得到了 index.php

我猜是因为我使用了重写规则。请帮助我,我花了数周时间来了解 htaccess。我确实发现了关于堆栈溢出的类似问题,但没有一个对我有帮助。

提前致谢

【问题讨论】:

  • 我知道当您在规则开头写 : ^(.*) 时,它会将您请求的任何 url 重定向到 index.php?aa= 。您需要将 index.php 重定向到一个虚拟文件夹,例如:^blog/
  • 非常感谢 Cyrillus,在获得 aa 值后,我实际上有超过 10 个页面使用 php 在 index.php 上包含,我不想纠正 30 行脚本,这是另一个原因我采取了上述步骤..你能再给我一个提示吗..
  • 好吧,如果你以上面的例子为例,任何带有 blog/xx/xx/xx 的 url 都会像这样重建你的 url(在服务器端,请注意你的): index.php?=aa&XX=&XX =&XX= 任何不是这样开始的 url,都不会被重定向。如果 aa 表示您的博客,请为他们保留博客,并为其他 ?bb 、 ?cc 生成其他模式(无论您使用什么。:)
  • 我将 /app/ 添加到我的 url 以打开索引文件,所以我的最终路径是 localhost/sandboxapp/app。它运行良好并且还加载了 css,然后我想在 index.php 中放置一个标头位置,它正在重定向连续循环。我是否必须使用 pregmatch 将 /app/ 添加到路径中。还有其他正确的方法吗..
  • 你救了我的命 Cyrillus.. 非常感谢

标签: css .htaccess mod-rewrite


【解决方案1】:

在得到 Cyrillus 的帮助后我找到了最好的方法

RewriteEngine On
RewriteRule \.(css|jpe?g|gif|png)$ - [L]

RewriteCond %{REQUEST_URI} !^/forum/
RewriteCond %{REQUEST_FILENAME} !-l [OR]
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .?- [S=3]
RewriteRule ^app/(.*)/(.*)/(.*)?(.*)$ index.php?aa=$1&bb=$2&cc=$3 [NE,L,QSA]
RewriteRule ^app/(.*)/(.*)?(.*)$ index.php?aa=$1&bb=$2 [NE,L,QSA]
RewriteRule ^app/(.*)?(.*)$ index.php?aa=$1 [NE,L,QSA]

最终输出:

url: example.com/app/question/100/relative-paths
PHP: example.com/index.php?aa=question&bb=100&cc=relative-paths

感谢 Cyrillus

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-03
    • 2011-12-06
    • 1970-01-01
    • 2023-01-09
    • 2011-09-19
    • 1970-01-01
    • 2018-08-20
    • 1970-01-01
    相关资源
    最近更新 更多