.htaccess URL 重写问题(脚本不加载) [英] .htaccess URL Rewrite Problem (Scripts don't load)

查看:17
本文介绍了.htaccess URL 重写问题(脚本不加载)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 .htaccess 中遇到了 URL 重写问题.

这是我的重写代码:

RewriteEngine on重写基数/RewriteRule ^terms(.*) terms.php [L,nc]RewriteRule ^formgate/([a-zA-Z0-9]{64,64})$ formgate.php?g=$1 [L,nc]

当页面加载时,它运行正常,除了我的所有样式表、javascript 文件等都没有正确加载.如果您检查错误,它会指出 404 not found 并且 $ 未定义.谢谢

解决方案

这个问题通常来自于使用相对 URL 到样式表或 javascript 文件并将其与虚拟目录结构结合.如果你有这样的重写:

RewriteRule ^articles/(.*)$articles.php

然后你有一个像这样的网址:

http://example.com/articles/2011/03/14/title-of-the-article

其中包含 HTML:

无论您的文件 artices.php 是否位于您的 Web 目录的根目录中,浏览器都对此一无所知,因为 mod_rewrite 的内容对浏览器来说是隐藏的.所以它会要求文件:

http://example.com/articles/2011/03/14/title-of-the-article/css/general.css

这显然不存在.所以我建议指定相对于您的网络根目录的 URL.例如:

注意 URL 前面的 /.

请注意以下几点:当您的 URL 以 / 字符开头时,它表示相对于网页根目录的路径.查看此示例目录结构:

<预><代码>//index.html/一个文件夹/文件夹/样式表/afolder/stylesheets/first.css/afolder/stylesheets/second.css/文件夹/图像/afolder/images/a.jpg/其他文件夹/otherfolder/something.html

I am having a problem with an URL rewrite issue in .htaccess.

Here is my rewrite code :

RewriteEngine on
RewriteBase /

RewriteRule     ^terms(.*)  terms.php [L,nc]
RewriteRule ^formgate/([a-zA-Z0-9]{64,64})$ formgate.php?g=$1 [L,nc]

When the page loads it functions properly except that all of my stylesheets, javascript files, etc do not load properly. If you check the errors it states 404 not found and that $ is not defined. Thanks

解决方案

This problem often comes from using relative URL's to stylesheets or javascript files and combining it with a virtual directory structure. If you have a rewrite like this:

RewriteRule ^articles/(.*)$ articles.php

Then you have a URL like:

http://example.com/articles/2011/03/14/title-of-the-article

which contains the HTML:

<link rel="stylesheet" type="text/css" href="css/general.css" />

no matter if your file artices.php resides in the root of your web directory the browser knows nothing about it as the matters of mod_rewrite are hidden from the browser. So it will ask for the file:

http://example.com/articles/2011/03/14/title-of-the-article/css/general.css

which obviously does not exist. So I recommend specifying URL's relative to your web root. For example:

<link rel="stylesheet" type="text/css" href="/css/general.css" />

Mind the / in front of theURL.

Please note the following: when your URL starts with a / character it means a path relative to the root of the webpage. Check out this example directurey structure:

/
    /index.html
    /afolder
        /afolder/stylesheets
            /afolder/stylesheets/first.css
            /afolder/stylesheets/second.css
        /afolder/images
            /afolder/images/a.jpg
    /otherfolder
        /otherfolder/something.html

这篇关于.htaccess URL 重写问题(脚本不加载)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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