.htaccess RewriteEngine breaks CSS/ Images



我成功地将我的脏url重写为干净的url,但这也破坏了我的图像和样式文件夹:(

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /www/index.php?page=$1&cmd=$2 [L]

添加以下重写条件,以防止重写导致现有文件和目录的url,例如您的JS, CSS和图像文件。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)$ /www/index.php?page=$1&cmd=$2 [L]

如果你的图片仍然没有显示出来,可能是因为你包含了这样的图片:<img src="./img/myimage.png" />。如果现在将浏览器指向http://site.com/page/hello/,浏览器将在这里寻找图像:http://site.com/page/hello/img/myimage.png。然而,图像实际上在这里:http://site.com/img/myimage.png .

所以你必须像这样包括你的图像:<img src="/img/myimage.png" />(域名之后的一切)。这将迫使浏览器寻找ìn正确的位置。(假设您的目录结构是这样的)

这可能是因为你的样式现在也要index.php了。我的意思是

/styles/myStyle.css -> /www/index.php?page=styles&cmd=myStyle.css

所以你可能应该通过重写一些url来修复这个问题。像这样的东西?

RewriteRule ^pages/([^/]*)/([^/]*)$ /www/index.php?page=$1&cmd=$2 [L]

最新更新