我正在清理我的网址。
假设我有一个网址 http://www.example.com/index.php?id=12345&name=some-name
我只是用mod_rewrite更改了它以使其看起来更好
http://www.example.com/blog/12345/some-name
现在的痛苦是页面索引.php没有加载任何图像,css文件和锚链接都坏了。
我的图像的位置是 http://www.example.com/images/12345/some-image.jpg
我在我的网站上使用了相对路径。
现在我需要更改所有页面上的链接吗?.htaccess 可以为我做一些技巧吗?请帮忙。
这是我的.htaccess-
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^blog/(.*)/$ index.php?id=$1 [NC,L]
RewriteRule ^blog/(.*)/(.*)$ index.php?id=$1&name=$2 [NC,L]
看看我添加的第二行。
RewriteEngine On
RewriteRule '.(css|jpe?g|gif|png|js|css|htm|html|mp3|wav|ico)$ - [L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^blog/(.*)/$ index.php?id=$1 [NC,L]
RewriteRule ^blog/(.*)/(.*)$ index.php?id=$1&name=$2 [NC,L]
这基本上是说,如果您在URL中看到此扩展名,只需将其直接传递给文件即可。
RewriteCond 仅适用于下一个规则,而不是下面的所有规则。您的第二条规则不是避免存在的目录和文件。试试这个
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^blog/(.*)/$ index.php?id=$1 [NC,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^blog/(.*)/(.*)$ index.php?id=$1&name=$2 [NC,L]
啊...!
我只是在我的标题中添加了这个小行,它解决了我所有的问题:)
<base href="http://example.com/ />
现在我想...为什么我的脑海里花了这么长时间才想到这个:)