Hi eruna
First, WELCOME to SitePoint!

Originally Posted by
eruna
I'm trying to make site.com/category/sub-category
redirect to www.site.com/category/sub-category
This is what I have so far:
PLEASE wrap your code in [code]...[/code] tags as that preserves the code when quoting for a reply.
Code:
RewriteCond %{HTTP_HOST} ^site\.com$ [NC]
RewriteRule ^(.*)$ http://www.site.com/$1 [R=301,L]
It works for the second level (site.com/category) but not the third (site.com/category/sub-category).
How would you modify this to work for the third level?
Thank You E
Your code is fine as is (although you should be warned about the
EVERYTHING
atom).
[standard rant #1]The use of "lazy regex," specifically the

EVERYTHING

atom, (.*), and its close relatives, is the NUMBER ONE coding error of newbies BECAUSE it is "greedy." Unless you provide an "exit" from your redirection, you will ALWAYS end up in a loop!
[/standard rant #1]
Code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site\.com$ [NC]
RewriteRule .? http://www.site.com%{REQUEST_URI} [R=301,L]
You could, perhaps, ensure that mod_rewrite is not in the comment mode by starting with RewriteEngine on and not using (.*) to capture text already available in the {REQUEST_URI} string but you already have an escape from the loop generally created by (.*) so no problem - it WILL capture everything in the path-to-file and simply repeat forcing the www.
Regards,
DK
Bookmarks