Redirect domains and subdomains with .htaccess - .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]

Related

Redirect ALL pages from an old Domain to a new page on a different domain

I am trying to use htaccess to redirect ALL pages from a domain to a specific page on a new domain. Yes, I completely understand we will loose SEO value this way.
I currently have a cpanel redirect that makes this url:
https://www.kiss1047.net/
go to this
https://mytown-media.com/stations/kiss-104-7-kxnc-fm/
but that doesn't get any of the internal pages to redirect. I would also like all internal pages (here is an example):
https://www.kiss1047.net/listen-live
to also go to:
https://mytown-media.com/stations/kiss-104-7-kxnc-fm/
I have tried a few things, but they always carry over the page url, ie above /listen-live/
https://mytown-media.com/stations/kiss-104-7-kxnc-fm/listen-live/
and that results in a 404.
is there some htaccess magic i can employ here?
In your .htaccess file .. Make a single entry that looks like:
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) https://mytown-media.com/stations/kiss-104-7-kxnc-fm/ [R=301,L]
This will direct ALL TRAFFIC (.*) to your other website .. Regardless of file name or directory .. And will not append the file/directory to the end of the URL .. IE listen-live
This is a 301 Permanent redirect [R=301,L] .. Which means once followed by Google, will be indexed as such .. Also will cache in users browsers so that the browser remembers the 301 instead of bouncing between websites as well.
This command in .htaccess redirects every page of your old domain to new domain's one specific URL.
RewriteEngine on
RewriteRule ^(.*)$ http://www.newdomain.com/ [R=301,L]
For your case:
RewriteEngine on
RewriteRule ^(.*)$ https://mytown-media.com/stations/kiss-104-7-kxnc-fm/ [R=301,L]
In result:
https://www.kiss1047.net/listen-live
will be redirected to:
https://mytown-media.com/stations/kiss-104-7-kxnc-fm/

Getting subdomain to redirect and follow CNAME

i have migrated a domain from olddomain.com to newdomain.com...i want all URLs to redirect from olddomain.com to newdomain.com. I got this part working. My issue now is that I have a CNAME on newdomain for help.newdomain.com -> externaldomain.com (uses referrerURL to land on proper page)
I need to redirect help.olddomain.com -> help.newdomain.com -CNAME-> externaldomain.com
Is this possible with .htaccess rewrite? if not, is there another way to skin it?
Yes, a 301 .htaccess rewrite should do it. Put this in the server that currently serves help.olddomain.com:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^help.olddomain.com [NC]
RewriteRule ^(.*)$ http://externaldomain.com/$1 [L,R=301,NC]
If you really want it to hit help.newdomain.com first, use that in the RewriteRule instead.

URL rewriting .htaccess help needed

Assume I have two domain names example1.com and example2.com. I forwarded example1.com to example2.com using the domain forwarding on hosting panel. It works fine but when I go to example1.com/sub it just shows example2.com as the URL. I want it to show example2.com/sub. I tried URL rewriting but no luck so far. It just keep loading and shows nothing. Am I missing something ?
This the rule I used.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^example1.com$ [NC]
RewriteRule (.*) http://www.example2.com/$1 [R=301]
First make sure that you've cleared your browser's cache. If it's a 301 redirect your browser will cache the redirect. Then make sure to turn off your hosting panel's forwarding.
Other than that, your rule should work fine, assuming it's at the top of the htaccess file in example1.com's document root.
In RewriteCond rule you use !, which mean negate the result of the condition.

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]

Resources