开发者

.htaccess URL Rewrite Problem (Scripts don't load)

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜