问题
I have a small problem with rewriting urls. I have this really simple rewrite rule:
RewriteEngine on
RewriteRule ^(home|apps|news|research|contribute)/$ index.php?page=$1
However, whenever I call my website like this, e.g. website.net/home/ it correctly rewrites the rule and shows the corresponding page but also messes up the css and images since it rewrites the urls for them since they are relative paths in the code. Is there any easy way to prevent this from happening?
回答1:
This is because of the rewritten urls. when the url is example.com/home/ apache thinks /home/ is a directory and appends /home/ in front of all relative urls.
To solve this, You can add the following base tag in head section of your webpage :
<base href="/">
Related : Seo Friendly Url css img js not working
回答2:
Add this following line in your .htaccess
file
RewriteRule \.(gif|jpg|css|js|inc\.php)$ -[L]
来源:https://stackoverflow.com/questions/37645424/url-rewriting-with-htaccess-and-relative-paths