htaccess rewrite to create friendly urls
The following rule works, but it changes t开发者_运维百科he URL in the address bar, which is not intended.
RewriteRule ^network/(.*)$ http://www.example.com/network.php?networkUrl=$1 [L]
The following rule redirects, the URL stays the same, but all the images, includes in the network.php file become referenced incorrectly...
RewriteRule ^network/(.*)$ network.php?networkUrl=$1 [L]
Is there a way to make this work?
This is because your browser interprets paths as relative.
To solve this reference your images and CSS with absolute paths, i.e. <img href="image.jpg" />
becomes <img href="/image.jpg" />
Same applies for css so
<link href="stylesheets/foo.css" media="print" rel="stylesheet" type="text/css"/>
becomes
<link href="/stylesheets/foo.css" media="print" rel="stylesheet" type="text/css"/>
In this way all resources links works as expected when referenced from any depth as /foo/bar/baz/script.php
and so on.
Setting a base href HTML tag in your page could help too:
<base href="http://www.domain.com/" />
then all your relative images, stylesheets or javascript files will be relative to this base href.
http://www.w3.org/wiki/HTML/Elements/base
精彩评论