So I’m stuck. I’m not very good with mod_rewrite or regular expressions in general, and this is giving me problems.
I need to redirect url’s like
domain.com/view/Some_Article_Name.html
to
domain.com/index.php?p=view&id=Some_Article_Name
The rule that I have now works fine, but it also rewrites for all my stylesheets and images and stuff that shouldn’t be rewritten.
RewriteRule ^view/([^/]*)\.html$ /index.php?p=view&id=$1 [L]
It should only redirect pages that start with domain.com/view/*.I imagine that all I need is a rewritecond, but I can’t seem to make one that works. Got any idea what I need to add to this to make it work without writing a rewriterule for every individual file?
RewriteCond %{REQUEST_FILENAME} ^(.*)\.html [NC]
RewriteRule ^view/([^/]*)\.html$ /index.php?p=view&id=$1 [L]
is my rewrite statement for this.
I doubt that your stylesheets and images are being affected by this rule. Because that would mean your stylesheets’ and images’ URL paths end with
.html. Because otherwise the rule won’t be applied.I rather suppose that you’re using relative URL paths to reference the stylesheets and images like
./css/style.cssorimages/foo/bar.png. Such relative URL paths are resolved from a base URL path. And that is the URL path of the current document.Your original URL path had just one path segment and all relative URL paths worked when starting at that point. But now you introduces another path segment (
/view) and relative paths are resolved from that path. So./css/style.cssorimages/foo/bar.pngis now resolved to/view/css/style.cssand/view/images/foo/bar.pnginstead of/css/style.cssand/images/foo/bar.pnglike it was with/index.php.The solution: Use absolute URL paths like
/css/style.cssor/images/foo/bar.pngto reference your external resources. With such URL paths the base URL can have any path and the resources are always getting resolved correctly.