tagged: apache-mod_rewrite

Apache: Redirect HTTP traffic to HTTPS

If you want all the HTTP traffic to be rerouted through HTTPS, you need to specify a mod_rewrite rule:

<VirtualHost *:80>
 ServerName  example.com
 RewriteEngine on
 ReWriteCond %{SERVER_PORT} !^443$
 RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]

This is saying take all traffic going to port 80 that's not on port 443 (yeah...) and rewrite it to a https:// url, ignoring case (NC), sending a moved permanently error message (R=301).
Now specify the vhost entry for HTTPS:

<VirtualHost *:443>
  ServerName  example.com

  SSLEngine on
  SSLCertificateFile your_cert.pem
  SSLCertificateKeyFile your_private.key

The above is a normal SSL vhost entry, specifying the certificate and private key for your HTTPS site.

unix apache ssl https apache-mod_rewrite

Apache 2: Redirecting to another site with mod_rewrite

If you place a file like this in your /etc/apache2/sites-available/somesite.conf

ServerName www.example.org
ServerAlias example.org
ProxyPass / http://localhost:1234

Then when you hit http://www.example.org/ Apache will then rewrite your request to http://localhost:1234 on your server, yet the browser won't know the difference , it will only know it's talking to http://www.example.org
Should your server perform any redirects, let's say http://localhost:1234/initial redirects to http://localhost:1234/redirected, then the following will ensure that http://localhost:1234/redirected is converted into http://www.example.org/redirected

ProxyPassReverse / http://localhost:1234/

To do the same with your cookie domain path use the following.

ProxyPassReverseCookieDomain http://localhost:1234 http://example.org    
ProxyPassReverseCookiePath http://localhost:1234 http://example.org   

You should see https://httpd.apache.org/docs/2.2/mod/mod_proxy.html for more details.

apache unix apache-mod_rewrite

Tomcat 7: Apache HTTPd 2.2 integration with virtual-hosts

First install and enable the mod_jk module for Apache
apt-get install libapache2-mod-jk
a2enmod jk

Then create a workers file. A worker is a process that will connect you to a tomcat instance. We're creating the workers.properties file at /etc/apache2/workers.properties.

We're giving it a name (will be used later), saying we're using the ajp13 connector to connect to tomcat 6 and above instances, saying it's on localhost and saying we're listening on port 8009 (we'll set tomcat listening on this port in a little while.)
Now in your apache.conf file, add:
JkWorkersFile /etc/apache2/workers.properties
JkShmFile /var/log/apache2/mod_jk.shm
JkLogFile /var/log/apache2/mod_jk.log
JkLogLevel info
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "

Here we point to our workers file, setup some file locations to be next to each other and set some logging information.
Finally edit the /etc/tomcat7/server.xml to accept these ajp13 connections. Uncommend this line:
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

The only thing left is to setup your virual host file. Here's an example of a virtual host file you should have in /etc/apache2/sites-available/blar
<VirtualHost *:80>

  JkMount / worker1
  JkMount /* worker1

  RewriteEngine on
  RewriteRule ^/(.*)$ /YOUR_DEPLOYMENT_NAME/$1 [L,PT]


Note we're pointing all the files that hit the root of our virual host to our worker via the JkMount command.
Generally, our tomcat servlets or jsp pages are prefixes with the name of the deployment file. Hello.war would be prefixed with Hello/. To get around this the RewriteRule gets around this by rewriting anything going to the root by transparently adding the deployment name.

tomcat apache apache-mod_rewrite

Apache 2: Using mod_write

First enable mod_rewrite with
a2enmod rewrite

Then add this to your available sites config
RewriteEngine On
RewriteRule .*index\.php.* /index.html [R]

That will rewrite all the files with index.php in them to index.html

unix apache apache-mod_rewrite

Page 1 of 1