Can I configure Apache on my laptop to forward to real site for all requests except one? - apache

I want to run some local tests on a site I have. The site is accessible at www.mysite.com. I want one particular file to be fetched from my local machine. I thought I could maybe achieve this by
installing Apache locally
adding 'localhost www.mysite.com' to my hosts file
configure Apache to forward all requests to www.mysite.com except for requests for the particular file www.mysite.com/myapp/myfile.css, which should be served from the Apache web server running locally.
Firstly I am not sure whether that set-up would work - in the case where a file is requested that is not my special case, the request would be forwarded to www.mysite.com/... , but would that then (because of the entry in my hosts file) go back to my local Apache server and into some infinite loop?
Secondly (and only relevant if the above is not true), how would I configure Apache to do that? I guess I need a ProxyPass but I'm having trouble figuring out exactly what.
Thanks for any help.
Paul

I don't think you'll be able to do this the way you're suggesting as you'll never be able to perform a lookup to proxy to www.mysite.com if you've defined it as localhost.
You could create another domain in your hosts file, say local.mysite.com and host the desired website files there and proxy everything else to www.mysite.com:
<VirtualHost *:80>
ServerName local.mysite.com
DocumentRoot ...
<Directory ...>
...
</Directory>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/myapp/myfile.css
RewriteRule ^(.*)$ http://www.mysite.com/$1 [P]
</VirtualHost>
Or if www.mysite.com works directly using the IP (i.e. not via virtual hosting) you could point localhost to mysite.com and use the real IP in the rewrite proxy.
<VirtualHost *:80>
ServerName www.mysite.com
DocumentRoot ...
<Directory ...>
...
</Directory>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/myapp/myfile.css
RewriteRule ^(.*)$ http://1.2.3.4/$1 [P]
</VirtualHost>

Related

Set urls for redirect to a specific url without rewrite

I'm building a webscheduler that have a multitenant structure, what I'm trying to do is assign a custom url that point to my application to each buyer.
So essentially when a user buy a license from me, I'll create a custom url on my webserver like this:
http://webserver/foo.scheduler.com/login
where foo is the name of the user that has buyed the license, and scheduler is a default part of the url, another example with more buyers:
http://webserver/foo.scheduler.com/login
http://webserver/foo2.scheduler.com/login
http://webserver/foo3.scheduler.com/login
essentially there are three buyers (my customers), each custom endpoint allow me to identify the correct database credentials, 'cause in my logic each tenant have a specific db, for more data organization.
Actually my application is located to this endpoint:
http://webserver/scheduler
I want to know if is possible point all custom urls to http://webserver/scheduler, without rewrite the url in the browser, so for example when the user go to http://webserver/foo.scheduler.com/login in the true is http://webserver/scheduler/login, but the user still continue to see http://webserver/foo.scheduler.com/login.
How can do this? In my .htaccess, available inside the root of the application folder I've this content:
RewriteEngine On
RewriteBase /webscheduler/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
this allow me to rewrite the base path to the index, and shunt the trace to the specific controller.
Happy to help you with this.
Get a valid SSL certificate for *.scheduler.com. You are going to need that if you're going to get this to work. Are you sure you want to use HTTPS? Your other URL is not HTTPS. Then you will need to set up your virtual host for *.scheduler.com to work properly with that certificate. Only having:
<VirtualHost *:443>
ServerAlias *.scheduler.com
DocumentRoot "/var/www/html/progetti/scheduler"
</VirtualHost>
Is not going to be anything like enough. You need all the mod_ssl stuff setting up in there as you have with the other virtual host. You could just use that default HTTPS host instead of adding another one, and modify it.
The first thing to do is get your hosting working for https://*.scheduler.com/ and then just point it at the right place.
What do you mean your endpoint is http://webserver/scheduler? This is not a valid domain name. Please clarify what you mean by that and I will update my answer with more information. Is the code on the same server?
--
Update
So to do this without SSL, add the following to your "000-default.conf", after what is currently there:
<VirtualHost *:80>
ServerAdmin localhost#gmail.com
ServerName www.scheduler.com
ServerAlias *.scheduler.com
UseCanonicalName off
DocumentRoot /var/www/html/progetti/scheduler
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/html/progetti/scheduler>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
--
Update
To make http://webserver/foo.scheduler.com work and serve /scheduler, add this to the VirtualHost that was already there. Not the new one added above, the original one at the top.
RewriteEngine on
RewriteRule ^(/[^./]+\.scheduler\.com)(?:$|/(.*)$) /scheduler/$2
Let me know any problems. If you would prefer to put it in your .htaccess it will need updating.
Note: I'm taking literally your statements that the app is using http:// and the clients will use https:// URLs. Also I'm assuming that the clients are hitting the same server as the one that hosts the app.
Probably the simplest way to do this is to configure one VirtualHost for your actual application and a separate one for the other URLs.
So assuming your application lives in /var/www/html/scheduler, then your existing VirtualHost looks like:
<VirtualHost *:80>
ServerName webserver
DocumentRoot "/var/www/html"
</VirtualHost>
You would need to add change your conf.d/ssl.conf to have something like:
NameVirtualHost *:443
<VirtualHost *:443>
ServerAlias *.scheduler.com
DocumentRoot "/var/www/html/scheduler"
</VirtualHost>

Rewrite spare domains to main domains with .htaccess

We have a site with an English and Spanish version, each on a different domain. We also have a few spare domains for each language which we'd like to redirect to the language's main domain.
Specifically:
estadiosfutbol.net/..., estadiosfutbol.org/... and estadiosfutbol.info/... should all redirect to https://estadiosfutbol.com/...
worldfootballstadiums.com/..., worldfootballstadiums.info/..., worldfootballstadiums.org/... and worldfootballstadiums.net/... should all redirect to https://worldstadiums.football/...
I'm struggling with the rewrite rules so any help would be greatly appreciated.
There are two ways this can be done. The first is the simpliest, but is not always practical.
First Method
This method does not require HTACCESS files. In your Apache server configuration you just need to add ServerAliases for each of the domains that you want it to handle. (You must make sure all the domains are pointing at the same machine)
The Code
NameVirtualHost *:443
<VirtualHost *:443>
ServerName estadiosfutbol.com
ServerAlias estadiosfutbol.info estadiosfutbol.net estadiosfutbol.org
DocumentRoot /www/domain
</VirtualHost>
<VirtualHost *:443>
ServerName worldstadiums.football
ServerAlias worldfootballstadiums.com worldfootballstadiums.net worldfootballstadiums.info worldfootballstadiums.org
DocumentRoot /www/otherdomain
</VirtualHost>
Note: This will only redirect if the user tries to access the website using SSL. (eg ) If you want it to redirect all traffic from both port 80 and port 443 you would need to make separate virtual hosts and use the second method to achieve the redirection.
Second Method
The second way is a little more complicated, but works in almost all situations. There a two main steps that need to be carried out in order for this to work properly:
Make sure that whatever server software you are using is setup to be looking for all the domains. The server has to have a VirtualHost(Apache) that is listening for each domain in order for the next step to do anything.
Create a .HTACCESS file under each domains' root that looks similar to this:
The Code
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !estadiosfutbol.net$ [NC]
RewriteRule ^(.*)$ https://estadiosfutbol.com/$1 [L,R=301]
Note: You will need to change the third line on each domain to be the domain to rewrite from (eg estadiosfutbol.net/, estadiosfutbol.org/ and estadiosfutbol.info)
Note: Changing the forth line is all that is required for the separate domain.

How to block Rails 4 Passenger requests from other virtual hosts?

I just deployed to production a Rails 4 application using Passenger on Apache/2.4.7 (Ubuntu) and in 24 hours I found out some sites that are identical to my site.
They are using some kind of DNS forwarding because everything is working, including forms on the website.
This is my apache virtual host:
<VirtualHost mysite.com:80>
ServerName www.mysite.com
ServerAlias mysite.com
DocumentRoot /home/deploy/mysite/public
RailsEnv production
<Directory /home/deploy/mysite/public>
Allow from all
Options -MultiViews
Require all granted
</Directory>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www)\.
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I am not sure how to stop requests from the other domains, I have tried removing "Allow from all" and "Required all granted" but that would stop the website working even from the correct domain and these settings are required by Passenger to work.
I'm a bit stuck so if you have any suggestions I would be happy to try them.
Turns out all requests coming from non-existing virtual hosts are routed to your first available vhost.
In order to stop anyone for hijacking your website using DNS forward you need to have a default virtual host enabled that is loaded first (using the default name 000-default, would do the trick) and acts as a catch-all and pointed to a forbidden page or even better create a simple html page with some links to your site.

Redirecting New Domain Name to Server

I recently purchased a new domain name from 1and1.com and used their HTTP redirect option to point to the address of my server. Let's say, for example, the fresh domain is new.com and the established server is old.com.
I have it redirecting to old.com/new via 1and1's configuration page, which works, save for the fact that when I visit new.com, it changes the browser's URL to old.com/new. This is obviously not what I want to happen.
I've set up htaccess rules:
# BEGIN New.com
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^new.com
RewriteRule ^(.*) http://old.com/new [P]
</IfModule>
# END New.com
Likewise, I've done the Apache configuration of Virtual Hosts:
<VirtualHost *:80>
ServerName www.new.com
DocumentRoot /www/old/html/new/
</VirtualHost>
I then proceeded to flush my local DNS cache. Yet still, it persists in changing the address bar in my browser to old.com/new. What am I missing? Does it just need time to propagate or have I misconfigured / failed to properly set something up?
You need to change the 1and1's "new.com" DNS entry to point to the same IP that "old.com" is using. While the htaccess rule (which I assume is at the new.com document root) kind of does what you want, it requires the mod_proxy be loaded, which is something I doubt 1and1 hosting allows.
What you need to do is set it up such that when you go to a site like this and do a DNS lookup for new.com, you get the same IP as when you lookup "old.com".
On old.com's server, you have the vhost setup:
<VirtualHost *:80>
ServerName www.new.com
DocumentRoot /www/old/html/new/
</VirtualHost>
which should be all you need to at least access the contents in /www/old/html/new/.

Redirect subdomain to subdirectory

i've already read all topics here and on google about redirection but i'm a programmer and i cant get it going.
I have Apache 2.2 installed. The web root is C:\Apache\htdocs. My network admin set me up a local domain that points to the server with Apache. The domain is myPhpApp.ourcompany.local. And this subdomain works, it shows the Apache "It works" page.
Now i have a website in C:\Apache\htdocs\myPhpApp and i want Apache to redirect the myPhpApp.ourcompany.local to this directory. The URLs should stay while browsing the website always as myPhpApp.ourcompany.local for example: myPhpApp.ourcompany.local/index.php, myPhpApp.ourcompany.local/data.php and so on.
I dont know how to achieve this? Mod-rewrite, virtual hosts, combination of both?
i have got this and this does not work:
<VirtualHost myphpapp.ourcompany.local>
DocumentRoot /myphpapp/
ServerName www.example1.com
RewriteEngine on
RewriteRule ^/$ /myphpapp/ [R]
</VirtualHost>
In effect i get:
Forbidden
You don't have permission to access
/myphpapp/ on this server.
Can anyone help?
EDIT
Maybe i forgot to mention: i dont put this into the www root which is C:/Apache/htdocs becuase i have more apps in there.
i have 3 directories in thdocs: myphpapp, myoldapp, mytestapp. As a target i want to have 3 subdomains that point to each directory.
I think it will be a combination of Nikola's and cromestant's answers:
<VirtualHost *:80>
DocumentRoot "C:/Apache/htdocs/myPhpApp"
ServerName myPhpApp.ourcompany.local
</VirtualHost>
First of all, you don't need a rewrite rule for simple thing as this.
I assume you need following VirtualHost definition
<VirtualHost *>
DocumentRoot /myphpapp/
ServerName myphpapp.ourcompany.local
</VirtualHost>
"VirtualHost *" part tells Apache on which interface and optionally port to listen to.
ServerName tells which domain name will be used to identify this virtual host.
Document root in your apache config should point to your directory where you have the app, in what you stated in your question it should be
DocumentRoot C:\Apache\htdocs\myPhpApp
and that is all.
restart or reload your apache, and test.