301 redirect with domain both pointing to same directory - apache

I am currently moving from one domain to another. Both domains are add on domains in CPanel and point to the same sub-directory.
How can i set up 301 redirect that redirects all of the old domains pages to the same file in the new domain. For example olddomain.com/example to newdomain.com/example. Since both domains reside in the same sub directory the both utilize the same .htaccess file.
Since both domains share the same .Htaccess file I'm having trouble getting the 301 redirect to work.

Put the following in the .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^newdomain.com
RewriteRule /(.*)$ http://newdomain.com/$1 [R,L]
It will check what your host-header is, and will redirect anything to newdomain.com if that's not the current site.

Related

Redirect a domain request to a subfolder

I am trying to redirect a domain to a subfolder within the same server. Let me break it down on topics:
I have the domain domain.com
I created the subdomain subdomain.domain.com
I redirected (A record) subdomain.domain.com to the domain.com server's IP address
I am trying to create a htaccess file that redirects every user that comes from subdomain.domain.com to domain.com/aaa
I can't figure out how though. I suppose I need to create a rewriting rule but don't know how.
To Redirect sub.example.com to example.com/folder you can use the following Rule in your /public_html/. htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [NC]
RewriteRule (.*) http://example.com/folder/$1 [L,R=301,NE]

How to add a rewrite rule for domain and it's subdomains in Plesk 11.0.9?

I need to redirect a request file from all possible subdomains to one file which is located in top level domain:
^.example.com/robots.txt >> http://example.com/robots.txt
The robots.txt may or may not exist in the subdomain httpdocs, this rule must be exucuted first (we want to block all crawlers in advance).
The folder structure provided by Plesk 11.0.9 is:
example.com/
...
httpdocs/
subdomains/
sub1/
httpdocs/
example.com is owned by root.
Where can I add an apache rewrite rule that would be trigger for all subdomains?
Edit: I tested with ping doesntexist.example.com and the request does get directed to example.com which means teoretically there should be a point in processing to execute the rewrite logic.
Try placing this rule in /example.com/httpdocs/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.(example\.com)$ [NC]
RewriteRule ^robots\.txt$ http://%1/$0 [L,R=302,NC]

Redirect for path but only for specific domain

We have different domains that are hosted on our server. Recently one of the main sections of our site has been moved to another server and been given a subdomain:
http://www.mysite.com/store
Has been moved to
http://store.mysite.com
Within our apache VirtualHost we wanted to redirect all traffic from the old domain to the new one:
Redirect permanent /store http://store.mysite.com
The problem is, we have other hosted sites that are being redirected now:
http://www.othersite.com/store
http://api.greatsite.com/store
We don't want this. How can I only have apache do redirects if http://www.mysite.com/store which has the /store path, and ignore other domains with /store?
Use mod_rewrite based code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteRule ^/?store(/.*|)$ http://store.mysite.com [L,R=301,NC]

htaccess : how to redirect all files in a folder to non subdomain if accessed via a subdomain?

using htaccess, I need to remove the subdomain part of the url if a file is accessed in /folder/myfiles/
eg. I need to redirect
sub.domain.com/folder/myfiles/anyfile.php
to
domain.com/folder/myfiles/anyfile.php
any file found in /folder/myfiles/ if accessed via a subdomain must be redirected to the same file on the root domain.
background: the site uses subdomains for wordpress blogs on multisite. There is a 3rd party app installed to a folder on the root domain, the licence is only valid for the root domain and attempting to access it via a subdomain invalidates the licence so I need to make sure that any file in that folder must only be accessed via the root domain and not a subdomain.
In the folder's .htaccess:
RewriteCond %{HTTP_HOST} sub.domain.com
RewriteRule (.*) http://domain.com/folder/myfiles/$1
In the root .htaccess or server config:
RewriteCond %{HTTP_HOST} sub.domain.com
RewriteRule ^/?(folder/myfiles(/.*)?)$ http://domain.com/$1

How can I redirect all traffic from one domain to another with an .htaccess file?

Say I have a subdomain xxx.yyy.com running Apache. The files are stored in /home/someone/public_html/xxx.
What I want to do is redirect all requests to a domain name zzz.com which is using the same location for its files. (In other words, xxx.yyy.com and zzz.com are aliases for each other)
I just want people accessing zzz.com, so if someone goes to xxx.yyy.com they should be redirected to zzz.com.
Can this easily be done with a rewrite rule in an .htaccess file?
Try
RewriteCond %{HTTP_HOST} ^xxx\.yyy\.com
RewriteRule (.*) http://zzz.com/$1 [R=permanent,QSA,L]