Mario Rossi
Mario Rossi

Reputation: 59

Prevent CSS URL from htaccess rewrite

I'm creating a standard rewrite with .htaccess to do this

http://www.myurl.com/post/{id}/

RewriteRule    ^post/([0-9]+)/?$    post.php?id=$1    [NC,L]

Now, it works perfectly but all others external URL ( for example CSS and JavaScript ) do not work, rightly. Is there a way to prevent it without having to change all the url ? I searched on Google without success, any ideas ?

Upvotes: 2

Views: 2238

Answers (2)

anubhava
anubhava

Reputation: 785316

You will need an additional rule to fix css, js, images links:

# your rewrite rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^post/([0-9]+)/?$ post.php?id=$1 [QSA,NC,L]

# fix css, js, image links
RewriteRule ^post/(.+?\.(?:jpe?g|gif|bmp|png|tiff|css|js))$ /$1 [NC,L]

Alternatively make sure to just use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /

Upvotes: 4

56ka
56ka

Reputation: 1565

Simply exclude physical files from your condition

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule    ^post/([0-9]+)/?$    post.php?id=$1    [NC,L]

Upvotes: 4

Related Questions