If you're a webhosting company sysadmin, sooner or later you will be asked by application developer or some client to redirect from an Apache webserver to some other webserver / URL's IP, in a way that the IP gets preserved after the redirect.
I'm aware of two major ways to do the redirect on webserver level:
1. To redirect From Apache host A to Webserver on host B using ReverseProxy mod_proxy
2. To use Mod Rewrite to redirect all client requests on host A to host B.
There is quite a lot to be said and is said and written online on using mod_rewrite to redirect URLs.
So in this article I will not say nothing new but just present some basic scenarios on Redirecting with mod rewrite and some use cases.
Hope this examples, will help some colleague sys-admin to solve some his crazy boss redirection tasks 🙂 I'm saying crazy boss because I already worked for a start-up company which was into internet marketing and the CEO has insane SEO ideas, often impossible to achieve …
a) Dynamic URL Redirect from Apache host A to host B without changing domain name in browser URL and keeping everything after the query in
Lets say you want to redirect incoming traffic to DomainA to DomainB keeping whole user browser request, i.e.
Redirect:
http://your-domainA.com/whole/a/lot/of/sub/directory/query.php
Passthe the whole request including /whole/a/lot/of/sub/directory/query.php
so when Apache redirects to redirect to:
http://your-domainB.com/whole/a/lot/of/sub/directory/query.php
In browser
To do it with Mod_Rewrite either you have to add in .htaccess mod_rewite rules:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^your-domainA.com [OR]
RewriteCond %{HTTP_HOST} ^http://your-domainA.com
RewriteRule ^(.*) http://your-domainB.com/$1 [P]
or include this somewhere in VirtualHost configuration of your domain
Above mod_rewrite will make any request to your-domainA.com to forward to your-domainB.com while preserving the hostname in browser URL bar to old domain http://your-domainA.com, however still contet will be served by http://your-domainB.com
http://yourdomainA.com/YOUR-CUSTOM-REQUEST-ADDRESS
to redirect to
http://yourdomainB.com/YOUR-CUSTOM-REQUEST-ADDRESS
WARNING !! If you're concerned about your SEO well positioning in search Engines, be sure to never ever use such redirects. Making such redirects will cause two domains to show up duplicate content
and will make Search Engines to reduce your Google, Yahoo, Yandex etc. Pagerank !!
Besides that such, redirect will use mod_rewrite on each and every redirect so from performance stand point it is a CPU killer (for such redirect using native mod_proxy ProxyPass is much more efficient – on websites with hundred of thousands of requests daily using such redirects will cause you to spend your hardware badly …)
P.S. ! Mod_Rewrite and Proxy modules needs to be previously enabled
On Debian Linux, make sure following links are existing and pointing to proper existing files from /etc/apache2/mods-available/ to /etc/apache2/mods-enabled
debian:~# ls -al /etc/apache2/mods-available/*proxy*
-rw-r–r– 1 root root 87 Jul 26 2011 /etc/apache2/mods-available/proxy_ajp.load
-rw-r–r– 1 root root 355 Jul 26 2011 /etc/apache2/mods-available/proxy_balancer.conf
-rw-r–r– 1 root root 97 Jul 26 2011 /etc/apache2/mods-available/proxy_balancer.load
-rw-r–r– 1 root root 803 Jul 26 2011 /etc/apache2/mods-available/proxy.conf
-rw-r–r– 1 root root 95 Jul 26 2011 /etc/apache2/mods-available/proxy_connect.load
-rw-r–r– 1 root root 141 Jul 26 2011 /etc/apache2/mods-available/proxy_ftp.conf
-rw-r–r– 1 root root 87 Jul 26 2011 /etc/apache2/mods-available/proxy_ftp.load
-rw-r–r– 1 root root 89 Jul 26 2011 /etc/apache2/mods-available/proxy_http.load
-rw-r–r– 1 root root 62 Jul 26 2011 /etc/apache2/mods-available/proxy.load
-rw-r–r– 1 root root 89 Jul 26 2011 /etc/apache2/mods-available/proxy_scgi.load
debian:/etc/apache2/mods-avaialble:~# ls *proxy*
proxy.conf@ proxy_connect.load@ proxy_http.load@ proxy.load@
If it is is not enabled to enable proxy support in Apache on Debian / Ubuntu Linux, either create the symbolic links as you see them from above paste or issue with root:
a2enmod proxy_http
a2enmod proxy
b) Redirect Main Domain requests to other Domain specific URL
RewriteEngine On
RewriteCond %{HTTP_HOST} ^your-domainA.com
RewriteRule ^(.*) http://your-domainB.com/YOUR-CUSTOM-URL [P]
Note that no matter what kind of subdirectory you request on http://your-domain.com (lets say you type in http://your-domainA.com/My-monkey-sucks ) it will get redirected to:
http://your-domainB.com/YOUR-CUSTOM-URL
Sometimes this is convenient for SEO, because it can make you to redirect any requests (including mistakenly typed requests by users or Bot Crawlers to real existing landing page).
c) Redirecting an IP address to a Domain Name
This probably a very rare thing to do as usually a Domain Name is redirected to an IP, however if you ever need to redirect IP to Domain Name:
RewriteCond %{HTTP_HOST} ^##.##.##.##
RewriteRule (.*) http://your-domainB.com/$1 [R=301,L]
Replace ## with digits of your IP address, the is used to escape the (.) – dots are normally interpreted by mod_rewrite.
d) Rewritting URL extensions from .htm to .php, doc to docx etc.
Lets say you're updating an old website with .htm or .html to serve .php files with same names as old .htmls use following rewrite rules:. Or all your old .doc files are converted and replaced with .docx and you need to make Apache redirect all .doc requests to .docx.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*).html$ $1.php [NC]
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*).doc$ $1.docx [NC]
The [NC] flag at the end means "No Case", or "case-insensitive"; Meaning it will not matter whether files are requested with capital or small letters, they will just show files if file under requested name is matched.
Using such a redirect will not cause Apache to redirect old files .html, .htm, .doc and they will still be accessible again creating duplicate content which will have a negavite impact on Search Engine Optimization.
The better way to do old extensioned files redirect is by using:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.+).htm$ http://your-domainB.com/$1.php [R,NC]
[R] flag would tell make mod_rewrite send HTTP "MOVED TEMPORARILY" redirection, aka, "302" to browser. This would cause search engines and other spidering entities will automatically update their links to the new locations.
e) Grabbing content from URL with Mod Rewrite and passing it to another domain
Lets say you want zip files contained in directory files/ to be redirected from your current webserver on domainA to domainB's download.php script and be passed as argument to the script
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^files/([^/]+)/([^/]+).zip https://www.pc-freak.net/download.php?section=$1&file=$2 [R,NC]
f) Shortening URLs with mod_rewrite
This is ueful If you have a long URL address accessible via some fuzzy long hard to remember URL address and you want to make it acessible via a shorter URL without phyisally moving the files within a short named directory, do:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^james-brown /james-brown/files/download/download.php
Above rule would make requests coming to http://your-domain.com/james-brown?file=my.zip be opened via http://mysite/public/james-brown/files/download/download.php?file=my.zip
g) Get rid of the www in your domain name
Nowdays many people are used to typing www.your-domain.com, if this annoys you and you want them not to see in served URLs the annoying www nonsense, use this:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} ^www.your-domain.com [NC]
RewriteRule ^(.*)$ http://your-domain.com/$1 [R=301,NC]
That's mostly some common uses of mod rewrite redirection, there are thousands of nice ones. If you know others, please share?
References and thanks to:
How to redirect domain without changing the URL
More .htaccess tips and tricks – part 2
More helpful Articles
Tags: apache2, com, company, directory, domain name, host, make, php script, root root, say, search engines, url, URLs, website, www
Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20140207 Firefox/24.0 Iceweasel/24.3.0
One thing I forgot to mention in the article is to have StatusNet installed there is LAMP (Linux Apache MySQL PHP) needed to be installed as a pre-requirement
I've earlier blogged on that
View CommentView Comment