使用 .htaccess 和相对路径重写 URL [英] URL rewriting with .htaccess and relative paths

查看:49
本文介绍了使用 .htaccess 和相对路径重写 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在重写网址时遇到了一个小问题.我有一个非常简单的重写规则:

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

但是,每当我像这样调用我的网站时,例如website.net/home/它正确地重写了规则并显示了相应的页面,但也会弄乱 css 和图像,因为它为它们重写了 url,因为它们是代码中的相对路径.有什么简单的方法可以防止这种情况发生吗?

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?

推荐答案

这是因为重写了 url.当 url 为 example.com/home/ 时,apache 认为 /home/ 是一个目录并在所有相关 url 前面附加 /home/.

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.

要解决这个问题,您可以在网页的 head 部分添加以下基本标签:

To solve this, You can add the following base tag in head section of your webpage :

<base href="/">

相关:Seo Friendly Url css img js 不工作

这篇关于使用 .htaccess 和相对路径重写 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆