Apache Virtualhost Directory conditional redirect - apache

I just created a website with two environments as virtualservers - testing and production. As production server is open to everyone but I allowed only my IP to access testing environment:
<VirtualHost *:80>
DocumentRoot /home/xxx/www
ServerName testing.xxx.com
<Directory /home/xxx/www>
Order deny, allow
Deny from all
Allow from xxx.xxx.xxx.xxx
</Directory>
</VirtualHost>
The problem is that google has already indexed some of my testing environment pages and they are available in google results. I would like any IP but mine to be redirected to production server (xxx.com) while accessing testing.xxx.com. I would rather do it with apache.conf than .htaccess(because of git repositories conflicts). Is it possible to add a conditional redirect to apache config?

You can use mod_rewrite features in your httpd.conf Apache config file:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REMOTE_HOST} !^123\.456\.788 [OR] # exclude your first IP
RewriteCond %{REMOTE_HOST} !^123\.456\.789 # exclude your second IP
RewriteRule ^(.*)$ http://production-env.com/$1 [R=301,L] # redirection to production site
</IfModule>
Or you can put these declarations into <Directory> section of your vhosts config file.
Generally you can take advantage of mod_rewrite module to manage URL routing policies for your web server. Before using it make sure that this module is installed and activated in your Apache.

Related

Redirecting HTTP to HTTPS with Apache RedirectPermanent for sites that use modperl

I have an Apache 2.4 site that serves content that is generated by modperl via HTTPS. The relevant Location section is as follows:
<Location />
SetHandler modperl
PerlResponseHandler MyService
</Location>
I would now like to enable the same content via HTTP as well. The following /etc/apache2/sites-enabled/myservice.conf works to some extend in that it redirects URLs with trailing paths but not the "root" page (say, http://myservice.mycompany.com). I suspect this is (also) because this page is special in that it is backed by modperl.
<VirtualHost *:80>
LogLevel debug
ServerName myservice.mycompany.com
RedirectPermanent / https://myservice.mycompany.com
</VirtualHost>
So how can I use RedirectPermanent for redirecting from HTTP to HTTPS for an entire Apache sites, including those pages that are backed by modperl?
To redirect any path in your site, you probably want to use mod_rewrite. These rules should work:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
(I've removed the perl and mod_perl tags from this question. The rewrite happens long before the server cares about how it's going to serve the content.)

Letsencrypt virtual host vs htaccess on apache

I have a site that I was working to move to use the virtualhost configuration over an .htaccess. Figuring if I have the access I should.
The rewrite I have was for php slim framework to remove index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
When I made the changes of moving from .htaccess to the production server routes were failing. I realized that the rewrites were not being followed on the :80 virtualhost but rather what was specified in the :443 letsencrpyt virtualhost.
In a pinch I just moved the above re-write into the letsencrpyt and voila working.
It seems that certbot uses the :80 port to create the :443 so I cannot remove that and just go with the generated one. Right now I am just duplicating the logic between the two.
Question I have at this point for this type of re-write should I just leave it in the .htaccess? Or is there a way to update the ssl vhost with certbot when I make changes to the default port 80 one?
Considering your comments to the question I now see that your actual question is:
"How is it possible to share configuration directives between different hosts in an apache http server? "
This is easily possible using the Include statement:
<VirtualHost *:80>
# include the rules common for http and https
Include sites-includes/shared-config.inc
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
# include the rules common for http and https
Include sites-includes/shared-config.inc
</VirtualHost>
</IfModule>
Using that you can now define common configuration directives in the file sites-includes/shared-config.inc (or wherever you want to place it).

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

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>

mod_rewrite: redirect requests from localhost to remote server

I have a following scenario:
Remote server with some webapp running at http://remote/webapp
Local machine inside corporate network
Corporate proxy between them
Apache with mod_rewrite running on my local machine
I would like to have the mod_proxy rewrite every request like http://localhost/webapp?someparams into http://remote/webapp?someparams.
Currently I have the following httpd.conf:
DocumentRoot "C:/Apache2.2/htdocs"
<Directory />
RewriteEngine On
RewriteRule ^(.+) http://remote/$1
Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
</Directory>
Which results in mod_rewrite transforming http://localhost/webapp?someparams into http://remote/C:/Apache2.2/htdocs/webapp?someparams
How to configure mod_rewrite to handle it correctly?
Since it looks like you have access to vhost/server config, you should ditch mod_rewrite and just use mod_proxy:
ProxyPass /webapp http://remote/webapp
ProxyPassReverse /webapp http://remote/webapp
and get rid of the 2 mod_rewrite lines (which is redirecting, not proxying):
RewriteEngine On
RewriteRule ^(.+) http://remote/$1
Note that if you have cookies, you may need to reverse map their domains.using ProxyPassReverseCookieDomain.
Also:
The fact that windows absolute path appears in the URL is due to misconfiguration of the mod_rewrite and this is what I'm trying to avoid
This is not a misconfiguration with mod_rewrite. When you put rewrite rules inside a <Directory>, the filepath is used in the match instead of the URI-path. According to the mod_rewrite documentation
What is matched?
In VirtualHost context, The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html").
In Directory and htaccess context, the Pattern will initially be matched against the filesystem path, after removing the prefix that lead the server to the current RewriteRule (e.g. "app1/index.html" or "index.html" depending on where the directives are defined).
Thank you Jon for inspiration, finally mod_proxy + mod_rewrite worked:
# Global context
RewriteEngine On
RewriteRule ^(.+) http://remote/$1 [P]
ProxyPassReverse / http://remote/
I know that this is a simplified and coarse solution, but works for my purpose.

Running magento on other port than default 80

I have configured Apache virtual hosting on port 8080 to point to my magento website.
Listen 8080
<VirtualHost 6x.2x.6x.1x:8080>
ServerAdmin webmaster#localhost
ServerName domainname.com
ServerAlias *.domainname.com
DocumentRoot /var/www/sites/domain/
<Directory />
Options FollowSymLinks
AllowOverride all
Order allow,deny
allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/access.log combined
ServerSignature On
<LocationMatch ".*\svn.*">
Order allow,deny
Deny from all
</LocationMatch>
</VirtualHost>
When i go to the website www.domain.com:8080 the js, css, img and other things are not loaded because the port is not attached to the links
Here is a rewrite rule in magento .htaccess that does not seem to work:
<IfModule mod_rewrite.c>
############################################
## enable rewrites
Options +FollowSymLinks
RewriteEngine on
############################################
## you can put here your magento root folder
## path relative to web root
#RewriteBase /magento/
############################################
## workaround for HTTP authorization
## in CGI environment
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
############################################
## always send 404 on missing files in these folders
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
############################################
## never rewrite for existing files, directories and links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
############################################
## rewrite everything else to index.php
RewriteCond %{SERVER_PORT} !^8080$
RewriteRule .* http://6x.2x.6x.1x:8080/index.php [L]
</IfModule>
Is Rewrite-mod the right solution to run magento site on different port? If yes, where is my mistake in the current Rewrite rule?
thank you,
Margots
I manage a magento site where a live shop is running on one server on port 80 and a dev site is running on another server on port 3000. When copying the site from live to dev all I need to do is change two rows in the core_config_data table, having:
path="web/unsecure/base_url"
path="web/secure/base_url"
You need to add your port number at the end of the url and that is all. In my case those two rows look like this:
(config_id, scope, scope_id, path,
value)
(default, 0, web/unsecure/base_url,
,http://www.dev-server.com:3000/)
(default, 0, web/secure/base_url,
,http://www.dev-server.com:3000/)
Note that I don't have a certificate on my dev server so I am not using https as the secure base_url. If you wish to use https this setting should be changed to https and you should omitt the custom port at the end.
Modification of the standard .htaccess file is not needed, and should probably be avoided.
EDIT
If you don't have access to the DB you can try creating a php file which will modify the database using magento:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
require_once ( "app/Mage.php" );
Mage::app('default');
$db = Mage::getSingleton('core/resource')->getConnection('core_write');
$db->query("UPDATE `core_config_data` SET `value` = 'http://dev-server.com:3000/' WHERE `path` = 'web/secure/base_url';");
$db->query("UPDATE `core_config_data` SET `value` = 'http://dev-server.com:3000/' WHERE `path` = 'web/unsecure/base_url';");
?>
You need to put this file in the root magento folder or change the path to Mage.php if you put it somewhere else.
I have not tried it (becaus I don't want to kill my Shop), but I think you can just change the Port in the Magento Admin.
In System / Configuration / Web / Base URL. Just try to add the Port to the URL there. Does that work?
You cannnot rewrite a URL that Magento writes in it's HTML Output using mod_rewrite. The Request to the wrong URL (without the :8080 Port) won't even reach your server.
UPDATE:
Look at the Source of the HTML output (i.E. go to your Shop with your Webbrowser and press CTRL-U or whatever). Do tags like the following have the correct URL, including the port?
<script type="text/javascript" src="http://yourstore.com:8080/js/prototype/prototype.js"></script>
All the methods shown seem to use a very complex process to do this.
It is very simple, change the URL to include the port in Admin > System > Configuration > Web > Urls
There you can define base URL, skin and media URLs.
You are missing this:
NameVirtualHost 6x.2x.6x.1x:8080
Perhaps you just didn't copy here since your site is working?
If Magento is loading pictures from the wrong site, there's nothing you can do in your Magento's .htaccess file: picture request will never get there. They will go to whatever other web server you have running on port 80.
You can only have this problem if Magento is building absolute links that include protocol and host (a terribly pointless bandwidth waste IMHO). My advice is that you look at the application settings and see if there's a place to specify the site base URL (since the app doesn't seem to be able to find it by itself).
(I tried to access Magento's demo site but it requires registering.)
I had the same problem because i was trying to use nginx with apache togather and i changed the apache port to a different than port 80 so i can use nginx as proxy, i changed the configuration file on apache httpd.conf but seemed that my site was not working, but I cleared the dns catch of my mac, and also used another browser to open my site and it worked.