rewrite everything page on subdomain to subdomain - .htaccess

I want to create a rule who redirect everything what is behind subdomain to subdomain.
For example:
https://sub.domain.com/hkgfohkf
to
https://sub.domain.com/
This rule must be for each other subdomains.
I tried something like this:
RewriteCond %{HTTP_HOST}^(([a-zA-Z0-9_-]+).domain.com/ [NC]
RewriteRule ^(.*)$https://$1.domain.com [L,R=301]
But it doesn't work.
Do you know how I can do this?

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

Related

Have a universal php.ini redirect in htaccess

I was wondering on how to make a rewrite rule that redirects www.domain.com/php.ini to www.domain.com, in a way its usable for any domain.
The solution I am looking for is something like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
but to rewrite redirects www.domain.com/php.ini to www.domain.com

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]

How to redirect a subdomain to another subdomain using htaccess

I would like to redirect a subdomain to another subdomain that is not on the same domain.
Example:
subdomain.mydomain.com --> subdomain.myotherdomain.com
I've tried the following code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain.domain\.com$ [NC]
RewriteRule ^(.*)$ http://subdomain.myotherdomain.com [R=301,L]
The RewriteCond doesn't seem to work...
Could you guys help me?
Thank you,
Damien
Try it this way in your subdomain root htaccess.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain [NC]
RewriteRule ^(.*)$ http://subdomain.myotherdomain.com [R=301,L]

global htaccess redirect for specific subdomain

I'd like to redirect all domains like join.domain.com to signup.domain.com including all URI Params.
I'm not quite sure where to start with this as this should be one rule that blankets all join.* domains on my server.
RewriteEngine On
RewriteCondition %{http_host} ^join$ [NC]
RewriteRule ^(.*)$ http://signup.$1/$ [R=301,L]
Try his rule:
RewriteEngine On
RewriteCondition %{HTTP_HOST} ^join\.(.+)$ [NC]
RewriteRule ^(.*)$ http://signup.%1/$1 [R=301,L]

What is the .htaccess redirect code to do the following?

For argument sake, let's pretend:
My domain name is mydomain.com
I have subdomains such as svn.mydomain.com
I want:
If the person types in anything other than mydomain.com, such as mydomain2.com, it must redirect to mydomain.com
If the person types ANY domain WITHOUT a subdomain, it must redirect to www.mydomain.com, keeping in mind, this rule must not affect subdomains such as svn.mydomain.com
if the type in svn.mydomain2.com, it must redirect to svn.mydomain.com
Any help?
Solution so far:
RewriteCond %{HTTP_HOST} !^(www\.)?namhost\.com$ [NC]
RewriteRule (.*) http://www.namhost.com/$1 [R=301,L]
This will redirect anything that does not include namhost.com, to www.namhost.com. So for example: namhost.net --> namhost.com, www.namhosting.com --> namhost.com. Now I just need rules for:
Excluding "localhost" from being redirected to www.namhost.com
Change subdomain redirects to namhost.com. E.g: someSubDomain.parkeddomain.com must go to someSubDomain.namhost.com. A real example: ftp.namhosting.com --> ftp.namhost.com
Thanks!
RewriteEngine On
#check if it's not mydomain.com
RewriteCond %{HTTP_HOST} !^(www\.)?mydomain\.com$ [NC]
#check if it's not a subdomain
RewriteCond %{HTTP_HOST} !^(.*)\.([A-Za-z0-9\-]+)\.(.{2,3}) [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([A-Za-z0-9\-]+)\.(.{2,3}) [NC]
#check if it's not one of determind sub-domains
RewriteCond %{HTTP_HOST} !^(svn|sql|pdf)\.([A-Za-z0-9\-]+)\.(.{2,3})$
#Redirect request to mydomain.com
RewriteRule (.*) http://www.mydomain.com/$1 [R=301,L]
#check if it's sub-domain and it's not mydomain.com's subdomain
RewriteCond %{HTTP_HOST} !^(www\.)?mydomain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(svn|sql|pdf)\.mydomain\.com$
RewriteCond %{HTTP_HOST} ^(svn|sql|pdf)\.([A-Za-z0-9\-]+)\.(.{2,3})$ [NC]
RewriteRule (.*) http://%3.mydomain.com/$1
let me know, if it needs any changes!:-)
RewriteCond %{HTTP_HOST} !^(.*\.)?namhost\.com$ [NC]
RewriteCond %{HTTP_HOST} !^localhost$ [NC]
RewriteRule (.*) http://%1namhost.com/$1 [R=301,L]
I use something like this...
RewriteEngine On
RewriteCond %{http_host} ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
You could also redirect to https by doing something like this...
RewriteCond %{http_host} ^mydomain.com/login.php [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/login.php [R=301,L]
Your Rule1 does not have an explicit subdomain
If the person types in anything other than mydomain.com, such as mydomain2.com, it must redirect to mydomain.com
and so Rule2 appears to contradict the Rule1
If the person types ANY domain WITHOUT a subdomain, it must redirect to www.mydomain.com
i.e. mydomain2.com by Rule1 would go to mydomain.com but by Rule2, since it does not have a sub-domain, it would go to www.mydomain.com
I will assume that in Rule1 you meant anysubdomain.non-mydomain.com should go to www.mydomain.com. If not please clarify as the rules below are based on this assumption.
Try adding the following to your .htaccess in the root directory of your site
RewriteEngine On
RewriteBase /
#redirect any domain without subdomain (including mydomain), to www.mydomain
RewriteCond %{HTTP_HOST} !^[^\.]+\.[^\.]\..+$ [NC]
#assume you do not want to carry over querystring or path just go to the root
RewriteRule . http://www.mydomain.com/? [R=301,L]
#if it has a subdomain
RewriteCond %{HTTP_HOST} !^([^\.]+)\.([^\.])\..+$ [NC]
#that is not on mydomain
RewriteCond %2 !^mydomain$ [NC]
#and it is on the list of subdomains to redirect
RewriteCond %1 ^(svn|otherIncludedSubdomains)$ [NC]
#also assume no querystrings carried over here. If needed, remove the last?
RewriteRule . http://(%1).mydomain.com/? [R=301,L]
I am applying Occam's razor here, assuming that your conditions are mutually exclusive (hence [L] for [last] rule), and that redirection to a domain always means redirection to the document root. You should be aware that mod_rewrite is capable of much more.
If the person types in anything other than mydomain.com, such as
mydomain2.com, it must redirect to mydomain.com
would be
RewriteCond %{HTTP_HOST} !^mydomain\.com$
RewriteRule ^/.*$ http://mydomain.com/ [R,L]
If the person types ANY domain WITHOUT a subdomain, it must redirect to www.mydomain.com,
keeping in mind, this rule must not affect subdomains such as svn.mydomain.com
would be
RewriteCond %{HTTP_HOST} ![^.]+\.[^.]+\.[^.]+$
RewriteRule ^/.*$ http://www.mydomain.com/ [R,L]
and
if the[y] type in svn.mydomain2.com, it must redirect to svn.mydomain.com
would be
RewriteCond %{HTTP_HOST} ^svn\.mydomain2\.com$
RewriteRule ^/.*$ http://svn.mydomain.com/ [R,L]
Untested.

Resources