How to Redirect any subdomain request to main domain? - apache

I'm trying to redirect all subdomain requests for domain.com to www.domain.com even when the subdomain does not exist, for example if we have:
abc.domain.com to www.domain.com
Where abc can be any requests. Furthermore, that subdomain abc may be exists or not. But whatever it is, I want to redirect it to main domain.
And less important request is. How it is possible to keep the input address at the address bar and redirect to main domain?
It will be best for me if it is done by .htaccess
I use apache server.
Thank you.

Since you haven't specified the environment you use, here are some general tips:
First, there is a difference between redirecting:
The user types sub.domain.com into the browser's address bar, but is redirected to domain.com -> domain.com is in the address bar, as the user is now on domain.com
...and rewriting an URL in the background:
The user types in sub.domain.com and stays at this address. The user sees sub.domain.com but in the background some other page (in your case, that one under domain.com) is loaded and shown.
(Quickly explained.)
If you are using Apache, take a look at 301 redirects and url rewriting.

In addition to what Piedone said (which is on the HTTP server side), you also need to configure the DNS to have a catch-all for all subdomains, directing them all to your HTTP server.
This implicitly means that all possible subdomains will exist automatically.

Related

Showing the Previous URL after redirecting to another url

I have a retail application that is hosted at www.emenu.com. My clients have there on websites that are hosted on their server, like www.fastfood.com, www.freshlime.com, etc. There is a button on their website which redirects to www.emenu.com.
www.fastfood.com on a button click redirect to www.emenu.com
www.freshlime.com on a button click redirect to www.emenu.com like that,
Problem is that the client does not want to show my application URL in the address bar after redirecting.
ie, www.fastfood.com will redirect to www.emenu.com but it should show www.fastfood.com in URL.
I am using laravel for the development. Came across many solutions involving .htaccess like
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?emenu\.come$ [NC]
RewriteRule ^ http://www.fastfood.com%{REQUEST_URI} [L,NE,P]
But the solution doesn't work.
The way websites work is like this:
user enters URL into browser (or clicks a link to get there), say www.example.com/foo/bar
the browser/OS looks up the A or AAAA DNS record for www.example.com, this DNS record points to an IP address
the browser sends an HTTP request to that IP address, like:
GET /foo/bar HTTP/1.1
Host: www.example.com
the server at that IP address (hopefully) responds with some HTML
So you see, if the domain www.example.com is already pointed at your client's server, it can't also point at your server. And the URL in the browser's address bar will always show the URL it's currently actually loading, you can't mask that. If you want the contents of your site to appear on your client's domain name, then the server pointed to by the DNS record for that domain actually needs to return the HTML for your site. You pretty much have two options there:
Embed your site in an iframe on your client's page. Say www.example.com/emenu.html contains:
<iframe src="http://www.emenu.com"></iframe>
Then visitors going to www.example.com/emenu.html will see the content of your site (embedded into a site of your client).
Set up the web server for www.example.com to reverse-proxy (some of) your URLs. In Apache that can be done with something like:
ProxyPass "/emenu" "http://www.emenu.com/"
ProxyPassReverse "/emenu" "http://www.emenu.com/"
Any HTTP request for the URL path /emenu to www.example.com would be forwarded by that server to your server, and the response be presented to the visitor as if the www.example.com server had produced it itself.

Cloudflare and cPanel issue - Reidrects back to main hostname

So I setup a subdomain off my regular domain. I then make an A record on Cloudflare to point to the IP address of my server. However, whenever I go to the subdomain, it just takes me back to the main hostname. (So i have sub.domain.com and when I go to sub.domain.com in my browser, it redirects me back to domain.com)
What could be the issue? I've checked .htaccess.
I've had similar issues with cPanel and Cloudflare before where cloudflare won't properly direct to the proper subdomain.
This is very difficult to look at without knowing the actual domain or subdomain in question. We don't do anything that would put a redirect in by default, so it sounds like it may be an issue on your server directly.

Apache 2.2 disable unconfigured subdomains

There is a webserver which has example.com, www.example.com, my.example.com, shop.example.com, static.example.com. All sites except www.example.com is on https. My problem is when I type randomstring.example.com -> shop.example.com gets served. But I want nothing to be served. For example mail.google.com is a valid subdomain. if you type johndoe.google.com you see "This webpage is not available". How to achieve this via Vhost/.htaccess file.
It's not the part of apache. You should disallow DNS resolving for subdomains like *.example.com.
or
If you don't have access to the DNS server - you can create a virtualhost with blank index page and put this virtualhost directive on top of the all virtualhosts. It shouldn't be same as google's behavior (they are using 1st way), but at least you can protect shop.example.com visiting from randomstring.example.com

replacing domain names using .htaccess

I have a new website as http://abc.com/case_studies/casstudy20/.
I have an old website as http://xyz.com/clients/home.php?client=myclient
There are loads of case studies and stories under old domain that is xyz.com. Now that I am using abc.com is it possible to replace the URL, so it shows abc.com but access the code from the same old place.
meaning when my users goes to old site instead of xyz.com. It should say http://abc.com/clients/home.php?client=myclient. But runs from the same old xyz.com.
Is it possible to do it using htaccess.
Sort of. You can redirect all the traffic from xyz.com to the proper path on abc.com but this requires you to maintain control of both domains.
If you can, it's actually better to put this in the virtual host config for xyz.com then you don't need a complete configuration at all. Assuming you have permissions it will work in .htaccess as well though.
RedirectMatch ^(.*)$ http://www.abc.com$1
If you can keep control of both domains then you are looking for a http 301 redirect. You can do that by adding the following line to your htaccess file to redirect an entire website. Without control of your original domain it's impossible for your old xyz.com website to be routed to the appropriate server so your htaccess file will never have anything to redirect.
redirect 301 / http://abc.com/

Hiding a subdomain

I have a website on a subdomain, and would like to hide the fact that it's a sub-domain. How can I do so?
For example, the site is www.example.com, and it's a sub-domain of www.mydomain.com. I don't want people to know that example.mydomain.com is the same as www.example.com. How do I hide this? With .htaccess? How?
Thanks!!
www.example.com is impossible to be a sub-domain of www.mydomain.com. I think you mean they point to the same IP address and, therefore, they point to the same website (or there are some kind of redirect).
A domain is simply, example.com or mydomain.com. Examples of sub-domains of these domains are: www.example.com, mobile.example.com, whatever.mydomain.com and they could, or not, point to the domain root.
I'll present you an example:
Google App Engine let's you chose a appspot.com subdomain, for instance, myapp.appspot.com. If you call this subdomain you access your website. But people usually want to use their own domain, or a subdomain, for instance, myapp.mydomain.com. Both subdomains will return the same content. If you'd wish to disable the appspot.com subdomain, you may follow this solution.
You could use the 'proxy|P' (force proxy) flag.
Read the manual http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
Note: mod_proxy must be enabled in order to use this flag.