【问题标题】:Apache RewriteEngine CSS & RewriteRulesApache RewriteEngine CSS 和 RewriteRules
【发布时间】:2013-01-13 14:00:48
【问题描述】:

我在本地主机上使用重写引擎时遇到了一些问题。 我正在我的www/project1/ 文件夹中的一个站点上工作,我正在尝试使用重写引擎。但是我遇到了一些问题:

首先这是我的 .htaccess 文件:

RewriteEngine On  

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^profile$ ./index.php?p=userprofile 
RewriteRule ^profile/(.*)$ ./index.php?p=userprofile&id=$1 [L]

RewriteRule $(.*)$ ./index.php&c=$1

基本上我想重写 所有/profile 链接到index.php?p=userprofile
将所有/profile/5链接重写为index.php?p=userprofile&id=5

和所有其他人(例如/somepage)到index.php?c=somepage

当使用上面的 .htaccess 时,我首先得到一个 500 内部服务器错误。但是,当删除最后一个重写时,此错误就会消失。

删除最后一条规则后,又出现了另一个问题 当转到localhost/project1/profile 时,一切正常,但是当添加另一个参数时:localhost/project1/profile/5 我所有的 css 都消失了。

我的 CSS 包含如下:href="style/main.css"

因此文件位于localhost/project1/style/main.css

现在我尝试通过添加/ 使路径相对 但是 CSS 根本没有应用,index.phpproject1/profile 上都没有

有什么解决方案可以让我的 3 条规则发挥作用吗?

【问题讨论】:

  • 首先,为什么在所有重写路径的开头都有一个点?那些应该以斜杠/index.php... 开头,而不是./index.php... 并且/index.php&c=$1 应该是/index.php?c=$1。这些只是这里的错字还是在您的实际配置中?关于 RewriteCond,它仅适用于紧随其后的 RewriteRule,不适用于后续规则,因此如果要对其他两个规则进行有效文件检查,则需要在每个规则前面再次添加 RewriteCond它们(可能是您的 CSS 文件和其他 500 错误的问题)。

标签: apache .htaccess mod-rewrite


【解决方案1】:

最后一条规则是错误的。

RewriteEngine on

RewriteRule ^profile$ index.php?p=userprofile [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^profile/(.*)$ index.php?p=userprofile&id=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?c=$1 [L]

关于 CSS,你必须使用 absolute 路径。所以你需要href="/project1/style/main.css"

您也可以为此使用 .htaccess 规则,而不是更改所有网址。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*/style.css$ style.css [NC,L]

您必须将它放在 .htaccess 中的所有规则之前。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-11
    • 2011-12-03
    • 2021-12-19
    • 2015-07-01
    • 2016-09-01
    • 1970-01-01
    相关资源
    最近更新 更多