Reputation: 23
# Redirect all users to access the site WITH the 'www.' prefix
RewriteCond %{HTTP_HOST} !^www\. [NC]
**RewriteCond %{HTTP_HOST} !\.([a-z-]+\.[a-z]{2,6})$ [NC]**
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Ok, so in the above code I think the 1st line says "if the URL does not have www." and then the 3rd line says to "rewrite the URL with the www. as a 301 redirect", but the 2nd line I believe is to take into consideration subdomains and exclude them, but can anyone tell me what this !\.([a-z-]+\.[a-z]{2,6})$
says exactly?
Another question: if mod_rewrite
is enabled do I still need to add RewriteEngine On
to the top of the .htaccess
file? What happens if I don't?
Upvotes: 0
Views: 66
Reputation: 468
The regular expression on the second line matches, if the string:
!
does not have\.
a literal dot, followed by[a-z-]
multiple characters of the a-z,- range, followed by\.
another literal dot, followed by[a-z]{2,6}
between 2 and 6 characters from the a-z range$
before the end of the stringThe parenthesis "(..)" allow capturing of a matched substring. This can then be used in following expression/substitutions.
The NC
flag will cause the match to performed case-insensitive.
In your case this will trigger a rewrite if the incoming URL
www.
(1st line).hostname.tld
(2nd line)See https://regex101.com/ for a good playground to experiment with regular expressions.
Upvotes: 1