How to redirect non-www to www without hardcoding using .htaccess?
17,410
Solution 1
Prix almost had it. When you negate the RewriteCond
(with !
) it doesn't capture so %1
is empty. Two possible solutions:
Dummy RewriteCond
:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)$ [NC]
RewriteRule ^/(.*)$ http://www\.%1/$1 [R=301,L]
%{HTTP_HOST}
in RewriteRule
:
RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteRule ^/(.*)$ http://www\.%{HTTP_HOST}/$1 [R=301,L]
Solution 2
In summation, a clean, tested version of the code:
This works (for me) to redirect www to non-www
RewriteEngine On RewriteCond %{HTTPS} !=on RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Either one of these work (for me) to redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC] RewriteRule ^(.*)$ http://www\.%{HTTP_HOST}/$1 [R=301,L]
or
RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteCond %{HTTP_HOST} ^(.+)$ [NC] RewriteRule ^(.*)$ http://www\.%1/$1 [R=301,L]
Solution 3
RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.(.+)$ [NC]
RewriteRule ^(.*)$ http://www\.%2/$1 [R=301,L]
the !
means if it does not start with "www..." then send it to www.%1
which is the (.+)
Related videos on Youtube
Author by
MrWhite
Updated on September 17, 2022Comments
-
MrWhite over 1 year
RewriteEngine On RewriteCond %{HTTPS} !=on RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
...causes a perfect, non-hardcoding 301 redirect from "www to non-www", what would the exact opposite look like?
EDIT:
According to Prix' post I've changed the
.htaccess
file to the following:Options +FollowSymLinks <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{HTTPS} !=on RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC] RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L] </IfModule>
As already mentioned, this redirects to
http://www./
unfortunately. Who can help? -
George Norberg over 13 years
-
George Norberg over 13 yearsplease advise...
-
Prix over 13 yearstry adding `` before the dot on the url, i update the above code but in general it should catch %1
-
George Norberg over 13 yearsI've added the whole thing... anything wrong here?
-
George Norberg over 13 yearsTried your fix, but still doesn't work.
-
Misha Slyusarev over 13 yearsYepp but I was looking for a "one-fits-it-all", non-hardcoding rule. What I really don't understand is that rewriting from www to non-www works... but not the other way round!?
-
Prix over 13 yearsHmmm this one is harder then i expected it to be, the reason it does not receive the %1 is because the first rule is meant to catch what does not match with it so it doesnt capture anything on %1 to be used bellow, so you gona need an extra rule to detect what it got.
-
Prix over 13 yearsNot sure if this update will work aswell but give it a try and let me know, dont forget to clean you cache so it doesnt reuse pre-cached data to confuse the job. The above one might works for subdomains but might not for domain.com
-
MastaJeet over 13 yearsYour "final" answer won't work. It will remap example.com to www.com.
-
Prix over 13 yearswell all i can say is that you need a condition to match in order to have a filled %1 or %N depending on where you set your parathensis, i dont have a server to test these rules right now but i am sure i am close :P
-
Prix over 13 years
^(.+)$
this will catch subdomain aswell so %1 would do odd things, not a good way to go, same with your second rule. For example, if user typed subdomain.domain.com it would try to make subdomain.domain.com/$1 -
MastaJeet over 13 yearsNo, if you typed subdomain.domain.com it would make www.subdomain.domain.com which is exactly what is asked for. I tested these rules on a running apache.
-
Uatec over 13 yearsThank you, guys - you really made my day! Without the leading "/" in RewriteRule both solutions work for me. So, which one is preferred here, maybe less "performance-sapping"?
-
MastaJeet over 13 yearsUse the leading /. Without the leading / in the RewriteRule was causing / to appear twice. E.g., example.com/foo/bar was being rewritten to example.com//foo/bar
-
Uatec over 13 years@embobo: With my Apache configuration I cannot confirm this. Using the leading "/" causes a 403 error but it's working just perfectly without it -
example.com/foo/bar
getswww.example.com/foo/bar
. Btw: Would it be possible to exclude subdomains from the rule(s)?www.subdomain.example.com
looks a little strange to me... ;-) THX -
Uatec over 13 yearsAnd what's about forcing a trailing slash for every subfolder within the domain, e.g.
example.com/foo/bar
will be rewritten toexample.com/foo/bar/
- that would be a kind of "holy grail" IMHO... ;-) -
MrWhite over 5 years"Use the leading /." - You should only use the leading slash on the
RewriteRule
pattern if these directives are in a server or virtualhost context. In a directory context (or.htaccess
- as in the question), then the slash prefix should be removed as it simply won't match anything. -
Jeaf Gilbert over 4 yearsredirect www to non-www works well for me, +1 for dynamic domain name, thank you.