.htacees Redirect All Subdomains Except www - .htaccess

I'm trying to edit my .htaccess code to work for all the below conditions. Here is the .htaccess file that I have so far:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old_domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)\.old_domain\.com$
RewriteRule ^(.*)$ https://new_domain.com%{REQUEST_URI} [R=301,L]
Here is the functionality I'm looking for:
old_domain.com -> https://new_domain.com/ #This Works
old_domain.com/any_directory/ -> https://new_domain.com/any_directory/ #This Works
www.old_domain.com -> https://new_domain.com/ #This Works
any_subdomain_besides_www.old_domain.com -> https://any_subdomain_besides_www.new_domain.com/ #This does NOT work
How should I edit my .htaccess file so that last line works, along with all the other conditions? To be clear, if the subdomain on the old domain is www, then remove it on the new domain. However if it is any other subdomain besides www, keep it in and only change the domain. Currently it removes all subdomains. Thank you!

You can use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?old_domain\.com [NC]
RewriteRule (.*) https://new_domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.+)\.old_domain\.com [NC]
RewriteRule (.*) https://%1.new_domain.com/$1 [R=301,L]

Only old_domain.com with www subdomain will be redirect
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?old_domain\.com [NC]
RewriteRule (.*) https://new_domain.com/$1 [R=301,L]

Related

use htaccess to remove www from root and subdirectories

I am having an issue with my htaccess. I am trying to remove www. from my root and subdirectories. However, every example I have tried redirects to root.
IE www.example.com -> example.com But www.example.com/blog -> example.com
I need to remove www. from all directories and subdirectories, and keep the original path intended.
IE www.example.com -> example.com AND www.example.com/blog -> example.com/blog
I have used the following
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
and
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]
and it is not working. Any help would be appreciated.
(Edit- after asked if I hade code above it)
I used this and it worked correctly
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]
I did not know drupal changed the way .htaccess worked. I apologize for not including that I was using this CMS in my original question
Have you tried this?
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

.htaccess unmasked/forced wildcard subdomain redirect

I want to edit the .htaccess file to create an unmasked wildcard redirect of all subdomains to the main www subdomain.
i.e. *.domain.com => www.domain.com
e.g. www.example.domain.com => www.domain.com
When the person types in www.example.domain.com in the browser address bar, I want it to redirect unmasked so that the URL actually visibly changes to www.domain.com.
My present .htaccess file (shown below) forces the 'www' for all 'without www' traffic, i.e. http://domain.com becomes http://www.domain.com, but can't figure out what RewriteCond etc. commands to use for the above requirement.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^(.*)$ "http\:\/\/www\.domain\.com\/$1" [R=301,L]
Any help would be greatly appreciated! Thanks in advance.
Have your .htaccess like this:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
It is simple, try :
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,QSA,R=301]
You won't need your rule that adds the www also, so just remove it.

Remove wildcard subdomains but not existing ones with .htaccess

Hello!
I'm trying to set up my .htaccess file for wildcard subdomains, but I really have no clue how to do that.
I have "domain2" pointing to "domain1" as an alias, which is working perfectly, this is the code I'm using:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www)\.(.*)\.(.*)\.(.*) [NC]
RewriteRule ^(.*)$ http://%2.%3.%4/$1 [R=301,QSA,L]
RewriteCond %{HTTP_HOST} ^(.*\.?)domain2.co\.cc$ [NC]
RewriteRule (.*) http://%1domain1.co.cc/$1 [R=301,L]
I found the www redirect here btw: Optimize htaccess Wildcard Subdomain Code
Now, what I want is all non-existent subdomains to get removed and the ones that exist (like "blog.domain1.co.cc" to stay.
I hope someone can help me with this. Thanks!
RewriteEngine On
#no longer needed
#RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
#RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#don't redirect blog.example.com, forum.example.com and example.com
RewriteCond %{HTTP_HOST} ^((blog|forum)\.)?example\.com$
RewriteRule .* - [L]
#redirect the rest (including www.) to example.com
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
Try adding the following to your htaccess file.
#if these lines already exist, skip them
RewriteEngine On
RewriteBase /
#if its not www or discussions subdomain
RewriteCond %{HTTP_HOST} !^(www|discussions)\.domain1\.co\.cc$ [NC]
#redirect to www domain
RewriteRule .* http://www.domain1.co.cc%{REQUEST_URI} [R=301,L]
For the following question
redirects subdomains and subdirectories to the other domain, just like that: forum.old.com/thread/12038213 --> forum.new.com/thread/12038213
Try
RewriteEngine On
RewriteBase /
#if domain is old.com
RewriteCond %{HTTP_HOST} ^(.+)\.old\.com$ [NC]
#redirect to new.com
RewriteRule .* http://%1.new.com%{REQUEST_URI} [L,R=301]

Non WWW to WWW redirection using htaccess File

I am using Codeigniter-.
My domain currently does not redirect to www version.
For example if I type mydomain.com then it stays mydomain.com. I want to redirect it to www.mydomain.com.
If someone types mydomain.com/controller/method then it should be www.mydomain.com/controller/method.
Another problem: I already tried other solutions but the problem is when it redirects to www version, it automatically adds "index.php" in the URL. But when I type www in the domain name then it works fine, no "index.php" in the URL. This problem occurs only during the redirection.
Here is my .htaccess file (I've removed the redirection code)
RewriteCond $1 !^(index\.php|system|rpc_relay.html|canvas.html|robots\.txt)
RewriteCond $1 !^(sitemap\.xml|export)
RewriteRule ^(.*)$ /index.php/$1 [L]
Any help would be greatly appreciated.
To redirect from http:// to http://www. , and also remove the route file (index.php) in the url, put these lines on your htaccess :
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond $1 !^(index\.php|images|css|js|styles|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
the domain is :
domain.com
folder with direct access :
images|css|js|styles
hope this help
I've used the following before:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
</IfModule>
To redirect domain.com to www.domain.com, you could use the following rewrite rule. Please replace domain.com with your own domain name.
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
I don't know why there are such complex RewriteRules answers, even though Gobhi has provided a nice generic solution (= whatever the domain name is, it works).
Here's my solution.
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
RewriteRule (.*)/index\.php$ $1/ [QSA]
</IfModule>

Htaccess - Redirect domain.com to www.domain.com, BUT for different top level domains

A website of mine is accessible through different URL:
studienbuch.ch
studienbuch.at
studienbuch.com
I'd like to have them changed to www.studienbuch.tld, but keep the top level domain:
studienbuch.ch -> www.studienbuch.ch
studienbuch.at -> www.studienbuch.at
studienbuch.com -> www.studienbuch.com
How to handle that with .htaccess?
The following should work (untested):
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.studienbuch\.(ch|at|com)
RewriteRule ^(.*)$ http://www.studienbuch.%1/$1 [R=301,L]
Try
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Resources