【问题标题】:htaccess to redirect GET vars without touching css linkshtaccess 重定向 GET 变量而不接触 css 链接
【发布时间】:2013-01-05 06:47:20
【问题描述】:

好吧,我已经在 stackoverflow 上阅读了页面和更多页面的 htaccess 指南和问题,但是仍然我还没有找到这个问题的答案。

我希望将 URL www.domain.com/profile/1 重定向到 www.domain.com/profile.php?id=1,并将 sabe 重定向到 www.domain.com/profile/ranisalt到 www.domain.com/profile.php?seo=ranisalt

我正在使用这个重写规则,并且它在重定向方面效果很好:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^profile/([0-9]+)/ profile.php?id=$1 [NC,QSA,L]
(there is more, but not relevant)

问题是,CSS 位于文件夹 css/ 中,如果我通过 profile.php?id=1 直接访问它会正确加载(www.domain.com/css/style.css),但是当我访问掩码,它试图在 www.domain.com/profile/1/css/style.css 找到 css,但没有找到。

我一直用这个作为临时修复

RewriteRule ^(.*)/(css|js)/(.*).(css|js)$ css/$3.$4 [L]
RewriteRule ^(.*)/img/(.*).(jpg|png)$ img/$2.$3 [L]

但它看起来并不整洁和漂亮:(

我怎样才能让它正确地加载 CSS?

【问题讨论】:

    标签: .htaccess mod-rewrite redirect get


    【解决方案1】:

    您的相对 URI 基础已更改,因此当浏览器加载像 www.domain.com/profile/ranisalt 这样的 URL 时,指向 css 等内容的相对链接将在 URI 的前面附加 /profile/(因此转为 www.domain.com/profile/css/some.css)。在profile.php生成的HTML中,你需要通过在页面头部添加这个标签来添加一个URI base:

    <base href="/">
    

    【讨论】:

    • 它没有改变任何东西,它仍在尝试查找 /profile/css/(...)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    • 2013-09-19
    相关资源
    最近更新 更多