DNS and apache redirection - apache

I'm having issues to clearly manage my website redirection. Let say that I'd like to create a invisible redirect from www.siteA.com to www.siteB.com/content. Here you have what I'd like to have:
www.siteA.com --DNS--> www.siteB.com --Apache on siteB --> /wwww/content/siteA/
For the first part, I can do it using "CNAME" configuration at DNS level.
However, I'd like to have a kind of Apache rewrite rule on "www.siteB.com" machine in order to redirect to "/wwww/content/siteA/" only if we come from www.siteA.com DNS requests.
Any idea?

Did you look at apache virtual hosts http://httpd.apache.org/docs/2.2/vhosts/ ? It does exactly what you ask for as I understood it - at least the second part.

Related

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.

Subdomain redirect when using HTTPS

My website runs on WooCommerce in public_html. I have a Subdomain in a public_html/sub. When I try to access the sub using HTTPS it seems to redirect to /wp-admin. I'm using the default URL rewrites that come with WooCommerce. How can I fix this issue?
Look at the Apache Config / Vhosts etc.
Basically, search for *:443 in the configuration file (or [ipaddress]:443 if you have fixed IP addresses associated with your server).
This will explain the difference in behaviour.

Making a URL suffix to prefix

I have a url [Like this: hostname.com] and I want to make hostname.com/blog to blog.hostname.com. I am using Apache web server. Let me know if I need to provide more info!
This should work for you:
RewriteCond %{HTTP_HOST} ^(www\.)? hostname\.com$
RewriteRule ^([^/.]+)(/.*)?$ http://$1.hostname.com$2 [L,R=301]
If you're not just doing a redirect, and you're actually setting up a server on blog.hostname.com, then you would need to configure Apache to use another (virtual) host for each subdomain in extra/httpd-vhosts.conf. See http://foundationphp.com/tutorials/apache22_vhosts.php for a tonne of info on configuring Virtual Hosts.
Just a quick example from memory:
<VirtualHost blog.hostname.com:80>
DocumentRoot /srv/www/blog
ServerName blog.hostname.com
</VirtualHost>
This will mean that the virtual host on blog.hostname.com will display the same pages as hostname.com/blog. This is assuming your document root is /srv/www (ie a linux setup). If not, just insert the correct paths where necssary.
Wow, it's been a while since I first made my SO account and this question. Anyway, I've learned a good amount since then, and now know that what I was trying to accomplish was to make a subdomain. The other two answers are correct in that it will setup a subdomain... if I had a domain pointing to it. I was using a NoIP domain for a tiny AWS free tier VPS, and the free NoIP stuff doesn't let you make a subdomain. I assumed that it would magically work if I told my VPS I wanted a subdomain. Whoops!
Backstory aside, to make a subdomain you need to use your domain host to add a new record pointing to your server, and configure your server to accept that new subdomain. Adding a subdomain depends on your provider, but now I use cloudflare, so adding an A / CNAME record following this guide will work. Once you add the record, use the other two answers to make apache listen on that domain.
Thanks for the (almost) 5 years of answers to now, SO!

Url rewriting: url pointing to a different server than the one specified in the RECORD A DNS

I have the domain "www.first.com" which points x.x.x.x
I have the domain "www.second.com" which points to y.y.y.y
I want the to type in the browser "www.first.com" and get to "www.second.com" (on the y.y.y.y) but the url showed in the browser has to be the same!
If I put a .htaccess inside the x.x.x.x server with:
RewriteEngine on
RewriteRule index.php http://www.second.com
I get to second.com, but the url is changed to http://www.second.com. Is there anyway to achieve what I want to do?
1 - It's because I need to host a website on my server, but the domain doesn't point towards it and I MUST NOT CHANGE the dns.
2 - I can change some of the contents in the x.x.x.x server, but I can't neither change DNS or put the website directory inside it.
I was considering put a simple index.php in the x.x.x.x server, which make a eval(file_get_contents("www.second.com")): I know it's weird, but I can't get closer than that to the solution.
Apache mod_proxy might be the best way to achieve that.

help regarding setting up pseudo/fake subdomains on apache

First of all, sorry if I got the term 'pseudo subdomain' wrong.
what I am trying to achieve is this-
When someone registers on my application, they get a new url like..
yourname.myapp.tld
I dont want to use the subdomain system for this. To be frank, I dont know how the subdomains exactly work but it guess it requires a folder per subdomain inside the document root and then the server redirects the requests there.
Can this be achieved by doing something like -
when a visiter types any subdomain, (anything.myapp.tld), he is able to access myapp . In the index.php file i will explode the $_SERVER['HTTP_HOST'] to get the subdomain which i will store in session and will thereafter act as an identifier for that user. Ideally i wouldnt want to create any vhosts or add many lines to the hosts file. Can this be achieved with just one vhost?
Is this possible with mod rewrite or something ?
Yes you can archive this using wildcard that needs to be configured on both, the dns server and http server
On the dns a entry like this (installing dns on ubuntu https://help.ubuntu.com/10.04/serverguide/C/dns.html):
; wildcard subdomains are all directed to this IP
; of course this should be the IP of your web server
*.domain.tld. IN A 1.2.3.4
At apache an entry like this:
<VirtualHost 111.22.33.55>
DocumentRoot /www/subdomain
ServerName www.domain.tld
ServerAlias *.domain.tld
</VirtualHost>
What happens after is that everything.domain.tld will be going to your main folder so you can use the index.php to redirect it to the right place or even an htaccess using mod_rewrite.