Mod_Rewrite问题。 CSS&图像不再加载

时间:2014-03-15 11:21:37

标签: mod-rewrite

我正在网站上工作。我已经设置了mod_rewrite规则并且它们工作得很好。但是css和图像不再正确链接。我尝试过在网络上找到的各种方法,但似乎都没有。所以所有的帮助都表示赞赏。

File Structure 

assets -> css, images, js, header.php, footer.php, global.php
controllers -> login.php, register.php, etc.
functions -> Various functions
view -> login.php, register.php, etc.
.htaccess
index.php

我正在尝试创建一个更清洁的mvc结构化Web应用程序。 (比以前的应用程序更干净)

.htaccess

AddDefaultCharset UTF-8
ErrorDocument 404 /404.php 
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f 


RewriteRule ^login$ controllers/login.php [QSA,L]
RewriteRule ^logout$ controllers/logout.php [QSA,L]


RewriteRule ^register$ controllers/register.php [QSA,L]
RewriteRule ^register/(.+)$ controllers/register.php?ref=$1 [QSA,L]


RewriteRule ^news$ controllers/news.php [QSA,L]
RewriteRule ^news/(.+)$ controllers/news_inner.php?url=$1 [QSA,L]


RewriteRule ^(.+)/(.+)/ladder/(.+)$ controllers/ladder.php?platform=$1&game=$2&url=$3&    page=standings [QSA,L]
RewriteRule ^(.+)/(.+)/ladder/(.+)/(.+)$ controllers/ladder.php?platform=$1&game=$2&    url=$3&page=$4 [QSA,L]


RewriteRule ^(.+)/(.+)/tournaments$ controllers/tournaments.php?platform=$1&game=$2 [QSA,L]

RewriteRule ^(.+)/(.+)/tournament/(.+)$ controllers/tournament_inner.php?platform=$1&game=$2&url=$3&page=info [QSA,L]
RewriteRule ^(.+)/(.+)/tournament/(.+)/(.+)$ controllers/tournament_inner.php?platform=$1&game=$2&url=$3&page=$4 [QSA,L]


RewriteRule ^(.+)/(.+)/team/(.+)$ controllers/team.php?platform=$1&game=$2&url=$3&page=main [QSA,L]
RewriteRule ^(.+)/(.+)/team/(.+)/(.+)$ controllers/team.php?platform=$1&game=$2&url=$3&page=$4 [QSA,L]


RewriteRule ^profile/(.+)$ controllers/profile.php?url=$1&page=main [QSA,L]
RewriteRule ^profile/(.+)/(.+)$ controllers/profile.php?url=$1&page=$2 [QSA,L]


RewriteRule ^(.+)/(.+)$ controllers/game.php?platform=$1&game=$2 [QSA,L]

我正在使用同一个mod_rewrite,我正在一个单独的网站上使用,该网站目前正在运行并且功能齐全(没有CSS /图像问题)所以我不确定是什么问题。我已经尝试了解决方案,request_filename解决方案,都没有工作,所以我假设我的重写有问题。

哦,我忘了在头文件中提到所有的css文件都有一个绝对路径

<link rel='stylesheet' type='text/css' href='$url/assets/css/reset.css' />
<link rel='stylesheet' type='text/css' href='$url/assets/css/style.css' />

我现在也遇到了上次重写规则的问题

RewriteRule ^(.+)/(.+)$ controllers/game.php?platform=$1&game=$2 [QSA,L]

我有点理解为什么,但我所做的改变似乎并没有起作用。感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我看到的第一件事就是这部分代码

RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f

对于所有规则,,但仅限于第一个条件之后 我让你的htaccess更干净,试试这个

AddDefaultCharset utf-8
ErrorDocument 404 /404.php

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

# If this is an existing file or folder, do nothing and leave
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule . - [L]

# If we reach here, this means it's not a file or folder (avoid rewriting on your files like css, etc)

RewriteRule ^(login|logout|register|news)$ controllers/$1.php [QSA,L]
RewriteRule ^register/(.+)$ controllers/register.php?ref=$1 [QSA,L]
RewriteRule ^news/(.+)$ controllers/news_inner.php?url=$1 [QSA,L]

RewriteRule ^profile/(.+)/(.+)$ controllers/profile.php?url=$1&page=$2 [QSA,L]
RewriteRule ^profile/(.+)$ controllers/profile.php?url=$1&page=main [QSA,L]

RewriteRule ^(.+)/(.+)/ladder/(.+)/(.+)$ controllers/ladder.php?platform=$1&game=$2&url=$3&page=$4 [QSA,L]
RewriteRule ^(.+)/(.+)/ladder/(.+)$ controllers/ladder.php?platform=$1&game=$2&url=$3&page=standings [QSA,L]

RewriteRule ^(.+)/(.+)/tournament/(.+)/(.+)$ controllers/tournament_inner.php?platform=$1&game=$2&url=$3&page=$4 [QSA,L]
RewriteRule ^(.+)/(.+)/tournament/(.+)$ controllers/tournament_inner.php?platform=$1&game=$2&url=$3&page=info [QSA,L]
RewriteRule ^(.+)/(.+)/tournaments$ controllers/tournaments.php?platform=$1&game=$2 [QSA,L]

RewriteRule ^(.+)/(.+)/team/(.+)/(.+)$ controllers/team.php?platform=$1&game=$2&url=$3&page=$4 [QSA,L]
RewriteRule ^(.+)/(.+)/team/(.+)$ controllers/team.php?platform=$1&game=$2&url=$3&page=main [QSA,L]

RewriteRule ^(.+)/(.+)$ controllers/game.php?platform=$1&game=$2 [QSA,L]

我交换了一些规则,因为您使用的(.+)模式匹配所有内容(太大)并导致优先级问题。

我也注意到你每次都使用QSA旗帜,我怀疑你并不知道它的作用。我没有从您的代码中删除这些标记,但您可以找到有关它的更多信息here并查看是否需要它