Remove wildcard subdomains but not existing ones with .htaccess - .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]

Related

How to redirect only sites with specific domain name?

I need to redirect all sites that as a specific word to one site.
I try to explain myself better.
I have a domain, it's example name is example.com
What I need is when an user digit:
something.example.com
or
anothertext.example.com
or
blablabla.example.com
and many others it will be redirect to example.com
I found this code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule ^(.*)$ https://www.example.com [R=301,L]
But it redirect all my domains in that server.
I discover it because I create a new virtual host, with a new domain but I can't see it on the web because it automatically redirects on example.com
So, I thought that maybe if I specify the word: "example" I can solve the problem.
Sorry for my bad english.
Thanks for help.
Luca.
Check one of rules that you more prefer:
<IfModule mod_rewrite.c>
RewriteEngine on
# something.example.com/any to https://example.com/
RewriteCond %{HTTP_HOST} ^something\. [NC]
RewriteRule (.*) https://example.com/ [R=301,L]
# something.example.com/any to https://example.com/any
RewriteCond %{HTTP_HOST} ^something\. [NC]
RewriteRule (.*) https://example.com/$1 [R=301,L]
# something.example.com/any to https://example.com/, full subdomain with and without www
RewriteCond %{HTTP_HOST} ^(www\.)?something\.example\.com [NC]
RewriteRule (.*) https://example.com/ [R=301,L]
# something.example.com/any to https://example.com/any, full subdomain with and without www
RewriteCond %{HTTP_HOST} ^(www\.)?something\.example\.com [NC]
RewriteRule (.*) https://example.com/$1 [R=301,L]
</IfModule>

redirect to a wildcard subdomain with htaccess file

I already added a subdomain wildcard to my dns (*.domain.com) but now i can't get the rule right.
I want
subdomain.domain.com
to point to
domain.com/subdomain
my htaccess file is :
RewriteEngine On
RewriteCond %{HTTP_OST} ^(?:www\.)?((?!www\.)[^.]+)\.(domain\.com)$ [NC]
RewriteRule ^/?$ http://www.%2/%1 [R=302,L]
but i'm getting Internal Server Error.
How can i get it to work ?
If you want it to actually redirect then you can do this
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/subdomain/$1 [R=301,L]
If you don't want it to redirect and keep subdomain.example.com in the address bar then, you can do this.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com [NC]
RewriteRule ^(.*)$ /subdomain/$1 [L]

Swapping domain name with htaccess

I'm trying to migrate a website to another domain and hope to have requests on the old domain redirected to the new domain, no matter what they are, and regardless of how the URL is structured.
The code below in my .htaccess file does this for a domain and any directories/pages but does not cover the many subdomains I have.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !domain1.com$ [NC]
RewriteRule ^(.*)$ http://domain2.com/$1 [L,R=301]
How can I add to this in order to convert the following (for example):
http://something.domain1.com/directory/page.php?variable=something
http://something.domain2.com/directory/page.php?variable=something
Try:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
RewriteRule ^(.*)$ http://domain2\.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain1\.com$ [NC]
RewriteRule ^(.*)$ http://%1.domain2\.com/$1 [L,R=301]

htaccess mod rewrite ignoring forward slash

I've been searching and searching with no success for this simple htaccess redirect from a non-www URL to a www.
I have the following in the site's root htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
From what I've read this should work but instead of htp://domain.com/index.html going to htp://www.domain.com/index.html, it goes to htp://www.domain.comindex.html/ - Notice the ignored forward slash between .com and index.html
I have also tried the following rewrite conditions: (not all at once obviously)
RewriteCond %{HTTP_HOST} domain.com [NC]
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
Any help would be greatly appreciated.
NOTE: Domains would usually have 'http' but because of the stack overflow limit of urls, I've had to remove a 't' :)
Tested solution that just works (at least on all my production servers):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

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>

Resources