I'm trying to set up the .htaccess file of my site to redirect all urls with http://www to the equivalent without www. (My preferred domain is http://domain.com) I've seen two different chunks of code to do this and I was hoping to find out what the difference between them is. Only the last line has minor differences. RewriteEngine On RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC] RewriteRule ^.*$ http://domain%{REQUEST_URI} [R=301,L] Code (markup): vs. RewriteEngine On RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC] RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L] Code (markup):
2+2 = 4 3+1 = 4 They will do exactly the same but get there in a different way, although the first one should be domain.com in the last line as well just for the sake of clarity. For instance, with the URL: http://www.pie.com/cheese/donkey.html %{HTTP_HOST} contains www.pie.com %{REQUEST_URI} contains /cheese/donkey.html And for the URL: http://donkey.net/weeeeee.php %{HTTP_HOST} contains donkey.net %{REQUEST_URI} contains /weeeeee.php Both rewrite codes have a rewrite condition (i.e. the following rewrite rule only applies if this condition is true) that match the host against www.domain.com, which should be self-explanitory so we're only redirecting users if they are on the www domain. The first one uses a regex pattern (the period . matching any character and the * meaning repeated 0 or more times, ^ and $ indicating the start and end of the string respectively) to intercept all requests. The rewrite destination is then just domain.com, with the %{REQUEST_URI} appended. The second one uses the same regex pattern but surrounds it in parenthesis (). This captures the matched pattern into a backreference of the form $N (in this case, N=1 since its the first set of brackets). The rewrite destination then uses the captured pattern to redirect you to the same page but on the non-www domain. Hope that makes sense.