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

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.

Related

Disable browser access to server IP address

I have a server that I can access in browser through the domain name (secured with SSL) and the server IP address.
I would like to disable access with the server ip address:
http://123.45.678.901/ and https://123.45.678.901/
How can I do that ?
OK found the complete solution here via htaccess file and 2 rules :
RULE 1: Redirect all requests to secure HTTPS access (including ip request http://123.45.678.901 )
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://domain.com/$1 [L,R=301,QSA]
RULE 2: If the domain or subdomain is not exactly domain.com redirect to bare domain (mandatory to catch https://123.45.678.901/any-page for example)
Using redirect all wildcard subdomains to root domain
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteRule (.*) https://domain.com/$1 [L,R=302,QSA]
All tests results here :
http://123.45.678.901/ > now redirect to https://domain.com/
https://123.45.678.901/ > now redirect to https://domain.com/
http://123.45.678.901/any-page > 404 > https://domain.com/index.php?p=any-page
https://123.45.678.901/any-page > 404 > https://domain.com/index.php?p=any-page
What are you trying to accomplish by blocking the IP access to the website? It is really the same thing. We only use nice looking domain names or host names for the benefit of humans. Whether you type in the domain name or IP is the exact same thing as someone can simply ping your hostname and get the IP address anyway.
If you want to enforce SSL simply do it for the host so that all access attempts require SSL. You can use apache mod_rewrite to accomplish this: https://www.sslshopper.com/apache-redirect-http-to-https.html
What you're asking for may not be possible, unless I'm not understanding the problem correctly. It's like saying I want someone to be able to send mail to my house only using my address but not my postal code, when the postal code effectively gives you the address.
Anyway hope this helped.
If you want to make it possible for users to access your site (server) through the domain name while accessing the corresponding IP address is disabled in order to disable the access to phpmyadmin through the IP address, it is impossible and not a good way to achieve what you want.
Roughly speaking, domain name is the human readable and memorable form of IP address, and when we type the domain name (e.g., http://google.com) on the browser, the domain name is converted to corresponding IP address by the DNS (domain name service) server, and the browser tries to connect to the IP address given by DNS. Eventually, trying to connect via domain name and IP address internally works the same way.
To remove the access of phpmyadmin from the other users and attackers, configuring the access control is right way. Try:
Use secure passwords for mysql users
Limit the permission of the mysql users according to the purpose of the mysql users. (Using the root user for all purpose and application is not a good way.)
If you correctly configure the above points, attackers can't access your database even if they know the URL of phpmyadmin.
To make sure that a redirect takes place only when someone is browsing with the ip of the server, I did the following (Ubuntu 20.04 - commands and paths may differ, if you use another OS):
Create a noip.conf file in /etc/apache/conf-available/ folder with this content:
<If "%{HTTP_HOST} =~ /12\.34\.56\.78/">
RewriteEngine on
RewriteRule ^ http://my.domain.com [L,R=301]
</If>
Enable the configuration and restart apache:
a2enconf noip
apachectl restart
The above will not work if someone types in the ip using https. They will get a "Your connection is not private" message. Then if they click "Proceed to 12.34.56.78", they will get the first matching ssl enabled virtual host. Make sure that this host is the one you want it to be.
You should also check if other applications on the server are listening on alternative ports, since someone could type in 12.34.56.78:999.

IP address is shown in address bar instead of domain

I have a WAMP home web server up and running on a static IP and registered a domain with Namecheap, but I'm a bit shaky with DNS. At first I used URL Redirect and pointed it to my IP. This meant that when you typed in the domain (like example.com) it just redirected you right to my IP, replacing the domain name with it in the address bar. Now I'm trying to get the domain to show instead of the IP in the address bar, which I'm struggling to understand exactly how to do.
The latest thing I've tried which many people say to do is instead of using URL Redirect to use the A (Address) record type and point it to my IP, which I thought would finally fix my problems. Of course after 15 min or so when it all got updated I'm getting a 400 Bad Request with nginx under it in Firefox, and a blank page in Chrome. Now I'm getting blank pages in both. Did I do something wrong here? Do I need to edit something on the web server such as httpd.conf? Am I going at this completely wrong?
Yes you should do away with the redirect and instead create an "A record". The sub-domain entry would typically be, but is not restricted to "www". The record type "A" and destination/target would be your external IP address. Once you update this record it may take several hours before you notice it taking effecting, upon where on people typing your URL would be directed to your web server.
You will need to forward port 80 on your router to the server hosting WAMP.
Finally the WAMP server should be provided with your domain name so it knows which site to load. If use the VirtualHost file this will allow you to host multiple domains on your web server. To do this...
Uncomment the following line so it appears like below in your Apache httpd.conf, to allow Apache to use virutal hosts
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
Then locate the httpd-vhosts.conf file, should be found in your WAMP installation location, such as C:\wamp\bin\apache\apache*version_number*\conf\extra\
Add an entry for your site, altering the details to your own domain name and website location.
<VirtualHost *:80>
ServerName www.stackoverflow.com
ServerAlias stackoverflow.com
DocumentRoot "C:/websites/stackoverflow/"
ErrorLog "C:/websites/stackoverflow/logs/error.log"
CustomLog "C:/websites/stackoverflow/logs/access.log" common
</VirtualHost>
Now restart your WAMP server and give it a whirl.
Tip: If your server won't start after these changes, check that you have created the folder structure for the log files!
Solution described here could resolve this issue.
Most of the free dynamic dns providers, allow acquiring more than one free host name. If allowed you can solve the problem by getting a second name, e.g., mysite2.somefree.org.
Now, go and configure your free domain names in the dashboard of free provider in the following way (assume your IP is 188.165.15.29 and your server's listening to port 8085).
redirect mysite1.somefree.org to mysite2.somefree.org:8085
redirect mysite2.somefree.org to your dynamic IP, say, to 188.165.15.29
This also works when you are using Apache httpd server alone, not being part of WAMP. You do not need to tweak virtual host or any part of your server. You only configure inbound direction.
Use Forward with masking where you registered your domain. mine is GoDaddy.
in the forward settings, you will see this at bottom of the page. click Forward with masking and add the title you want them to see in the address bar of the browser when they go to your site. instead of showing your IP address

URL not redirected using Apache rewrite

I have been using apache server. I want to redirect some URL to another, eg. www.abc.com to localhost:8080/Home
I uncommented rewrite module in httpd.conf in conf folder of apache installation. Then I wrote rewrite rule like show below in httpd.conf file.
RewriteEngine On # Turn on the rewriting engine
RewriteRule http://www.abc.com http://localhost:8080/Home/
But nothing happened. It is simply opening abc.com as normal. There is no error message not even in log.
Can anyone suggest where the problem is?
You can only rewrite URLs that have the server as a host. Since you do not host www.abc.com, you cannot rewrite any of its URLs.
It's not the 'best' solution, but I use it at home.
Edit the 'hosts' file on your own PC to redirect. For example, mine redirects 'attic' to ip 10.0.0.5, my server in the attic. So when I type attic/myfolder, I get what I would normally get at 10.0.0.5/myfolder.
Your hosts default location can be found with a very quick google.
Not the best, as I say, but it works.
EDIT:
Okay, something.something, we'll call it xyz.com.
We need 2 things here;
a) your server must expect traffic from xyz.com
(this is just a config on the server, easy to achieve).
b) your browser must be pointed to your server when you type xyz.com.
Normally, when you type xyz.com into any browser, your PC will connect to a DNS server to find out where in the world xyz.com actually is (the DNS server returns an IP address). To inform the DNS servers that xyz.com should point to YOUR server, you need to pay to register the domain name with a registrar (unassigned domains aren't expensive). This is the best way, as every computer will now know how to get to your server by typing xyz.com. When you move your website to a hosted server, you go to your registrar's website and change the settings, saying "stop pointing to the IP of my home server and start poionting to the IP of my hosted server".
Or, if you don't want to do that, you need to tell YOUR PC to skip the DNS check, and you do that by modifying your hosts file as above. This will only work for you, but is enough for home testing purposes.
The third option is running your own DNS server, and manually telling it to override the world-wide settings for xyz.com. That way your browser would get your custom result when it checks the DNS server, and forward straight to your server. However, running your own DNS server is a complex undertaking, and is overkill for your current task.
In summary, from best option to worst:
1) Register your domain and point it home
2) Modify your hosts file to bypass DNS checking
3) Run your own DNS server, and override the settings for xyz.com
Hope I've been more clear this time :O)

DNS and apache redirection

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.

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.