使用.htaccess和相对路径重写URL

时间:2016-06-05 18:36:22

标签: apache .htaccess url-rewriting

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

RewriteEngine on
RewriteRule ^(home|apps|news|research|contribute)/$ index.php?page=$1

然而,每当我像这样打电话给我的网站时,例如website.net/home/它正确地重写了规则并显示了相应的页面,但也弄乱了CSS和图像,因为它重写了它们的URL,因为它们是代码中的相对路径。 有没有简单的方法可以防止这种情况发生?

2 个答案:

答案 0 :(得分:1)

(module hello racket (provide hello) (define-syntax hello (syntax-rules (in) ((_ name in world) (format "Hello ~a in ~a" name world)) ((_ in name) (format "Hello ~a in here" name))))) (require 'hello) (define in "inside") (hello "me" in in) ; => hello: bad syntax in: (hello "me" in in) 文件

中添加以下这一行
.htaccess

答案 1 :(得分:1)

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

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

<base href="/">

相关:Seo Friendly Url css img js not working