0

I'm in process of cleaning my URLs.

Say I have a URL http://www.example.com/index.php?id=12345&name=some-name

I just changed it to look better using mod_rewrite

http://www.example.com/blog/12345/some-name

Now the pain is the page index.php is not loading any images, css files and the anchor links are all broken.

Location of my image is http://www.example.com/images/12345/some-image.jpg

I have used relative paths through out my site.

Now do I need to change links on all my pages ? Can .htaccess do some trick for me ? Please help.

Here is my .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]
4

3 回答 3

2

看看我添加的第二行。

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 中看到此扩展名,只需将其直接传递给文件即可。

于 2013-01-31T17:34:38.703 回答
1

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]
于 2013-01-31T17:35:16.827 回答
1

啊……!

我刚刚在我的标题中添加了这条小线,它解决了我所有的问题:)

<base href="http://example.com/ />

现在我想......为什么我的大脑花了这么长时间才想到这个:)

于 2013-01-31T18:37:44.290 回答