URL rewriting with .htaccess and relative paths

烂漫一生 提交于 2021-01-28 18:10:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!