rewrite www. to non www without specifying the domain? - .htaccess

I was using this code to remove www from my domain:
# remove www
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
My problem is that I usually use subdomains and it doesn't work for them. (www.sub.example.com).
How do I rewrite to remove www whatever the domain is and even if it has subdomains?

Try this:
#********** Remove www from address **********
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC]
This is a bit more correct than the other solution.
Edit:
I removed [L] as it isn't applicable here.
Though in most cases [NC] non-case sensitive is recommended.

You could probably use capture groups and combine with rewrites... (Completely untested guess)
# remove www
RewriteCond %{HTTP_HOST} ^www.(.*) [NC]
RewriteRule ^(.*)$ http://$1 [R=301,L]

Related

using .htaccess to add www prefix

I have a site hosted on godaddy. uses apache.
I used this code in .htaccess to add www prefix to the domain automatically
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/../$1 [R=301,L]
but instead of 'www.example.com' it goes to 'www.example.com/web'
I just want to convert 'example.com' to 'www.example.com'
If you just want to convert example.com to www.example.com then you just need to use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=302,NC]
You can also lay it out like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]
Make sure you clear your cache before testing this. You will notice I've just the flag R=302. This is a temporary redirect, use this while you're testing. If you're happy with the RewriteRule and everything is working, change these to R=301, which is a permanent redirect.
solved by using this
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*)$ http://www.example.com/$1 [R=301]
RedirectMatch 301 ^/web/$ http://www.example.com/

htaccess ignore rewriterule for subdomain

here's my htaccess settings for redirecting urls which don't start with "www." (on my LIVE-environment):
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
i'm also running my mirrored testing environment on a subdomain called test.mydomain.com.
how can i disable this rule for it (as it would always redirect to the live environment)?
thanks
Just add test in your negative RewriteCond like this:
RewriteCond %{HTTP_HOST} !^(www|test)\.
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
Anubhava's answer worked for me with a small adjustment:
RewriteCond %{HTTP_HOST} !^(www|test)\. [NC]
RewriteRule ^(.*)$ http://www.example.org/$1 [R=301,L]

htaccess : combining permanent domain change and stripping of www

I I use the code below to strip the www from my (new) domain. Now I want to redirect the old domain tot the new one too. Can't get it to work. Is it possible to do both?
RewriteCond %{HTTP_HOST} ^www\.skiweather\.eu [NC]
RewriteRule ^(.*)$ http://skiweather.eu/$1 [L,R=301]
RewriteBase /
Just rewrite anything to your new domain:
RewriteCond %{HTTP_HOST} !=skiweather.eu [NC]
RewriteRule ^(.*)$ http://skiweather.eu/$1 [L,R=301]

Dynamic subdomains + defined subdomains via htaccess

I hate asking questions about mod_rewrite, but I can't seem to get these rules working properly. I had it setup and figured out once before, but a few years have passed and now it's just not playing nice and my mod_rewrite knowledge is back to uhhhh......
Essentially what I want to do is force www on the main domain, example.com. (or forcing no www would be even better, but I haven't remotely been able to get that working properly along with dynamic subdomains).
If my.example.com rules exist, then follow them accordingly
For anything else, ie: *.example.com, pass to another file to parse.
The rules I have currently handles forcing the www, and properly handles the dynamic subdomain, but completely ignores my.example.com rules, handling it as a wildcard subdomain. It seems I can get any one of my rules working properly, but when trying to get them working together it's just being a bastard. If anyone knows have to get this working properly it would be super appreciated.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# force www on main, force no www would be better.
RewriteCond %{HTTP_HOST} !^(.*)\.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
# my.example.com defined subdomain
RewriteCond %{HTTP_HOST} !^my\.example\.com$ [NC]
RewriteRule ^.*$ - [S=3]
RewriteRule ^$ /mydirectory/index.php [L]
RewriteRule ^category/([^/]+)/?$ /mydirectory/index.php?category=$1 [L]
RewriteRule ^submit/?$ /mydirectory/process.php [L]
# *.example.com
RewriteCond %{HTTP_HOST} ^(.*)\.com$ [NC]
RewriteCond %1 !^(www)\.examples$ [NC]
RewriteRule ^([^/]+)/?$ /subdomainparse/index.php?subdomain=%1&fakedirectory=$1 [L]
Try these rules:
# remove www on main
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
# my.example.com defined subdomain
RewriteCond %{HTTP_HOST} !^my\.example\.com$ [NC]
RewriteRule ^.*$ - [S=3]
RewriteRule ^$ /mydirectory/index.php [L]
RewriteRule ^category/([^/]+)/?$ /mydirectory/index.php?category=$1 [L]
RewriteRule ^submit/?$ /mydirectory/process.php [L]
# *.example.com
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$ [NC]
RewriteCond %1 !^www$ [NC]
RewriteRule ^([^/]+)/?$ /subdomainparse/index.php?subdomain=%1&fakedirectory=$1 [L]
The first rule will now redirect the +www.example.com* to example.com. And the third rule will catch any request to a non existing host.

Htaccess to force use WWW for mutlidomain

I found this code to force using www.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]
But i have many domain that pointed to same directory. So i need a version of this code for multiple domains. It must run on any domain, is it possible ?
This should work:
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*) http://www.%{HTTP_HOST}/$1 [R=301]
It will redirect all request without a subdomain to www.domainame.tld.

Resources