2

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 ?

2 Answers 2

4

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 /

Sign up to request clarification or add additional context in comments.

1 Comment

Or make sure that all your paths are relative to the site root (i.e. include /css/stylesheet.css instead of css/stylesheet.css in your HTML code).
4

Simply exclude physical files from your condition

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

5 Comments

Good answer, I would definitely change this, but anubhava's answer addresses the real problem.
That actually won't help if relative css paths is being used and path is becoming for ex. /post/css/style.css
Exactly, it is just the first step for having files accessible.
i tried in this way but it returns 404 error RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^post/([0-9]+)/?$ index.php?p=$1 [NC,L] Any ideas ?
You have to access your files by their real path, not using post/. If you want something with, refer to anubhava's answer ;) More explainations here : stackoverflow.com/a/15879058/1529139

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.