Setting mod rewrite in htaccess / all images lost
This seem to be an often question online, but most site's have old answers and or way out of date methods.
I'm using .htaccess to rewrite URLs for an SEO website.
Basically, the header link开发者_StackOverflow中文版s go like this:
<ul id="navigation">
<li><a href="index/" title="Home">Home</a></li>
<li><a href="softwares/" title="Softwares">Softwares</a></li>
<li><a href="about/" title="About">About</a></li>
<li><a href="contact/" title="Contact">Contact</a></li>
</ul>
So the full url would be: http://localhost.com/softwares/ or http://localhost.com/about/ and etc...
RewriteRule ^([^/]+)/$ $1.php [NC]
Now, this works for when routing the /$/ to the file, however, all images, href, and etc don't work / show. (because the url is technically pointing to a sub directory).
My question: What should we write or what should we do to fix those images, css, urls, and etc...
The “problem” here is that you’re using relative paths that are resolved differently on base paths with multiple segments:
base path | reference | resolved path
------------+-------------+--------------------
/ | img/foo.png | /img/foo.png
/index.php | img/foo.png | /img/foo.png
/index/ | img/foo.png | /index/img/foo.png
One simle way to solve this is using absolute paths instead:
base path | reference | resolved path
------------+--------------+---------------
/ | /img/foo.png | /img/foo.png
/index.php | /img/foo.png | /img/foo.png
/index/ | /img/foo.png | /img/foo.png
You can also change the base URL using the BASE
element. But note that this will affect all relative URLs.
精彩评论