Mathias Bosman
Mathias Bosman

Reputation: 63

Apache RewriteEngine CSS & RewriteRules

I got a couple of issues with using the rewrite engine on my localhost. I'm working on a site located in my www/project1/ folder and I am trying to use the rewrite engine. However I run into some issues:

First of all this is my .htaccess file:

RewriteEngine On  

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^profile$ ./index.php?p=userprofile 
RewriteRule ^profile/(.*)$ ./index.php?p=userprofile&id=$1 [L]

RewriteRule $(.*)$ ./index.php&c=$1

Bascially I want to rewrite all /profile links to index.php?p=userprofile
rewrite all /profile/5 links to index.php?p=userprofile&id=5

and all others (such as /somepage) to index.php?c=somepage

When using the above .htaccess I first of all get a 500 Internal Server Error. However when removing the last rewriterule this error goes away.

With the last rule removed, another problem rises When going to localhost/project1/profile everything works perfectly fine but when adding another parameter: localhost/project1/profile/5 all my css is gone.

My CSS is included as follows: href="style/main.css"

the file is thus located in localhost/project1/style/main.css

Now I tried making the path relative by adding a / But then the CSS isn't applied at all, not on index.php nor on project1/profile

Any solution to this so I can get my 3 rules to work?

Upvotes: 0

Views: 774

Answers (1)

n1xx1
n1xx1

Reputation: 2076

The last rule is just wrong.

RewriteEngine on

RewriteRule ^profile$ index.php?p=userprofile [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^profile/(.*)$ index.php?p=userprofile&id=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?c=$1 [L]

About the CSS, you have to use an absolute path. So you'll need href="/project1/style/main.css"

You can also use a .htaccess rule for that instead of changing all urls.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*/style.css$ style.css [NC,L]

You have to put it before all rules in your .htaccess.

Upvotes: 2

Related Questions