【问题标题】:htaccess slash loading resourceshtaccess 斜线加载资源
【发布时间】:2014-06-28 02:36:29
【问题描述】:
<IfModule mod_rewrite.c>
    # Enable rewrite engine
    RewriteEngine On

    #Removes access to the system folder by users.
    RewriteCond %{REQUEST_URI} ^app.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    # Some extra conditions
    # skip these paths for redirection
    RewriteRule ^img/(.*) public/assets/img/$1 [L]
    RewriteRule ^css/(.*) public/assets/css/$1 [L]
    RewriteRule ^js/(.*) public/assets/js/$1 [L]
    RewriteRule ^plugins/(.*) public/assets/plugins/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_URI} !^.*\.(jpg|css|js|gif|png)$ [NC]

    RewriteRule ^(.*)$ public/index.php?url=/$1 [QSA,L]

</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.

    ErrorDocument 404 /index.php
</IfModule> 

除了当我加载像 localhost/login/(尾部斜杠)这样的 URL 时,它没有加载资源之外,这种重写是有效的。当我使用localhost/login 时,它会加载所有图像和 CSS 文件。有人可以帮我解决这个问题。

【问题讨论】:

标签: php .htaccess resources rewriting


【解决方案1】:

这可能是因为您在 HTML 中使用了相对路径。就客户端 URL 而言,在 URL 的末尾加上一个斜杠,您实际上是在另一个子文件夹中。

要解决此问题,请在客户端代码中使用根相对(以斜杠开头)或绝对 URL。例如:

/img/picture.jpg

或者,如果您无法修改您的 URL,请在 head 部分中包含一个 base 元素,以指示浏览器相对 URL 是相对于什么。例如,让它们相对于文档根目录:

<base href="/">

或者,通过重定向到 .htaccess 中的非斜杠 URL,或在 index.php 中处理此问题,防止尾部斜杠返回有效文档。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-15
    • 2010-10-27
    • 1970-01-01
    • 2021-04-19
    • 2011-12-28
    • 1970-01-01
    相关资源
    最近更新 更多