Redirecting domain without visibility to the user - .htaccess

I have a server with 2 domains parked on it. For example abc.com and xyz.com.
The content of my CMS (WP) is on abc.com/content/.
I want to redirect user calling xyz.com to the abc.com/content while preserving the abc.com domain.
Other example:
If I call xyz.com/example-page it should redirect on abc.com/content/example-page without let user see the abc.com domain.
I have the possibility to use mod_rewrite and/or redirect by confixx panel.

You can put this code in your htaccess (which has to be in document root folder)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?xyz\.com$ [NC]
RewriteRule ^((?!content/).*)$ /content/$1 [L]
Don't forget to enable mod_rewrite and check if htaccess files are allowed in your Apache configuration

Related

Redirect domains and subdomains with .htaccess

Right now *.foo.com and *.bar.com are aliased to the same vhost with LAMP. Right now it, of course, shows the same content with every URL. But I don't actually want this to be what happens, and I want the other domains to actually redirect to foo.com. I'm assuming this is possible with the .htaccess file.
EDIT: I'm hosting this on my own server, so I'd also like the IP address to redirect to foo.com
Use a rewrite condition in your .htaccess to selected everything not for foo.com and redirect it then:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^foo\.com$
RewriteRule ^(.*)$ http://foo.com/$1 [R=301,L]

Redirect But Not Show The URL redirec :: htaccess?

I need to redirect some users, possibly to a subdomain, but I do not want them to know that they are in a subdomain. They should think they are in the main domain.
I believe this can be done with htaccess, but it is gibberish to me.
Can someone please throw me some bones?
When serving content from a different domain without redirecting the browser (thus changing the URL in the address bar) one of two things needs to happen. Either there is a file-path resolution to the other domain, or a reverse proxy must be set up (and it's pretty easy to do if mod_proxy is loaded).
It looks like you have your subdomain inside the document root of your main domain, which means this option will be viable. So if you want to it so when someone puts this URL in their address bar, http://domain.com/page, they get served the content in http://sub.domain.com/another-page, you'd simply add these rules to the top of the htaccess file in the document root of your main domain (public_html):
RewriteEngine On
RewriteRule ^page(.*)$ /subdomain_folder/another-page$1 [L]
Otherwise, the second option is to use mod_proxy:
RewriteEngine On
RewriteRule ^page(.*)$ http://sub.domain.com/another-page$1 [L,P]
EDIT:
I want to redirect some users, depending on their ip (geographic location) to another subdomain. And yes public_html is the root
You can check against the IP via the %{REMOTE_ADDR} variable:
RewriteCond %{REMOTE_ADDR} ^123\.45\.67\.89$
RewriteCond !^/subdomain_folder
RewriteRule ^(.*)$ /subdomain_folder/$1 [L]

Redirecting all domain aliases to one with htaccess

I have a few domain aliases, while I only want one of them to be actually used.
At the moment, I have two domains installed,
To redirect I'd like to use htaccess.
My domains currently work like so:
www.domain.com/index.html - Main domain's home page
www.secondDomain.com/index.html - Displays exactly the same home page as the main domain, but I want it to automatically rename the url to www.domain.com/index.html when it's used.
Thanks!
It is a simple matter of matching %{HTTP_HOST} not equal to www.domain.com and redirecting that to the canonical domain.
RewriteEngine On
# If the hostname is NOT www.domain.com
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
# 301 redirect to the same resource on www.domain.com
RewriteRule (.*) http://www.domain.com/$1 [L,R=301]

.htaccess mod_rewrite redirect subdomain to url, with preserving subdomain in url path

I made simple web page at http://www.domain.com/mobile/{rest of url}
and i want to have http://mobile.domain.com/{rest of url}
On other forums, ppl tolde me to move page, unfortunately it's not possible since that page is using other controllers and models from the rest of page and /mobile/ is not folder, it's name of codeigniter controller that is also defined buy some other htaccess rules
I created subdomain mobile dot domain.com and i want to redirect all traffic from it WITH keeping URL in this form: mobile dot domain dot com/{path}
Try adding this to your htaccess:
RewriteCond %{HTTP_HOST} ^mobile.domain.com$ [NC]
RewriteRule ^(.*)$ /mobile/$1 [L]
If you have other rewrite rules in your htaccess file you may want to add this before the other rules.

.htaccess old domain to new domain 301 redirect, but only the domain, not it's pages

Let's say I have a domain: A.com, and if someone goes to A.com I want to redirect to B.com.
But if they go to any page on A.com, e.g. A.com/wp-admin/index.php, there will be NO redirect. In short, I just want a simple top level old name to new domain name 301 redirect. (Any specific pages on A.com will be redirected to specific pages on B.com one by one).
Here you go:
RewriteEngine On
RewriteCond %{HTTP_HOST} =domain-one.com
RewriteRule ^$ http://domain-two.com/ [R=301,L]
This rule is to be placed in .htaccess in website root folder. If placed elsewhere some small tweaking may be required.
It will redirect hits to domain root only, e.g. http://domain-one.com/ and no other pages.
I have replaced A.com by domain-one.com and B.com by domain-two.com -- it is more realistic/understandable than A & B.

Resources