views:

29

answers:

2

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.

A: 

Are you saying that you want the css & images to live under the original file location (e.g. on htdocs/view/ where htdocs is your source root, but you want the url for the html to be rerouted to the index.php file?

If that is what you're saying, than what you need is a reverse RewriteRule as well, since the browser will pick any css/images relative to the redirected url (assuming relative image/css references in html).

Other than that, I agree with @David and can't how anything w/o view and .html could be matching.

Mike R
+1  A: 

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.css or images/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.css or images/foo/bar.png is now resolved to /view/css/style.css and /view/images/foo/bar.png instead of /css/style.css and /images/foo/bar.png like it was with /index.php.

The solution: Use absolute URL paths like /css/style.css or /images/foo/bar.png to reference your external resources. With such URL paths the base URL can have any path and the resources are always getting resolved correctly.

Gumbo