mod_rewrite and file paths
I am using mod_rewrite in my haccess to mak开发者_JS百科e clean URLs. The working directory is webroot/subdir
and htaccess resides in subdir
What I have works fine for the main folder
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^projects/([0-9]+)$ ?action=projects&id=$1
RewriteRule ^projects ?action=projects
RewriteRule ^clients ?action=clients
RewriteRule ^admins ?action=admins
RewriteRule ^settings ?action=settings
so those links would be like webroot/subdir/projects
or webroot/subdir/settings
and they work.
My problem occurs on the projects
line where I have to add an id. When I click a link that says something like: webroot/subdir/projects/284
, all my style sheets and images break. My CSS is setup like so:
<link rel="stylesheet" href="resources/css/reset.css" />
<link rel="stylesheet" href="resources/css/main.css" />
<link rel="stylesheet" href="resources/css/buttonPro.css" />
Is there a way around this? Am I doing my rewrite rules wrong?
Assuming that your resources
folder is also in webroot/subdir
, you need to make your links absolute paths. When the page changes to projects/([0-9]+)
, your working directory now becomes webroot/subdir/projects
even though the path is being rewritten, so it is trying to look up your style sheets at webroot/subdir/projects/resources
which doesn't exist.
Adding a /
to the beginning of your locations should work:
<link rel="stylesheet" href="/resources/css/reset.css" />
<link rel="stylesheet" href="/resources/css/main.css" />
<link rel="stylesheet" href="/resources/css/buttonPro.css" />
If your resources folder is outside the home/root directory, you will need to specify the full path to that directory, like this:
<link rel="stylesheet" href="/webroot/subdir/resources/css/reset.css" />
<link rel="stylesheet" href="/webroot/subdir/resources/css/main.css" />
<link rel="stylesheet" href="/webroot/subdir/resources/css/buttonPro.css" />
精彩评论