Apache Rewrite rules how to fix .com to .co.uk URL’s


The wonderful world of looking after web servers came up this week, there were two requirements. One to satisfy our marketing department and the other to stop some kids driving up the CPU of one of our web servers.

The marketing requirement was to allow both a .co.uk and a .com domain to be the same web site, this is easy DNS stupid!! but display the .com address in the URL and not the .co.uk

I new instantly that this meant learning Apache rewrite rules, I was later found that I was in for a world of pain and 4 hours later had a test bed environment that looks like it should work. There are a ton of references on the interweb and Google again is your friend.

A couple of places that are worth a note are the actual Apache docs, these were actually not to bad, in both trying to understand them and the examples that they gave.

Apache links

http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

https://httpd.apache.org/docs/trunk/vhosts/examples.html

 

Not everything should be done with re-write rules and one issue was discussed on a train journey this morning with a fairly knowledgeable guy. Not a clue what his name was but cheers mate 🙂

The following solutions to change the URL and to stop someone abusing our server is made up of both rewrite rules and virtual hosts.

OK in this example we will introduce a few test domains :-

www.test.com, www.test.co.uk, www.nonsense.eu

To try this out you need obviously a Apache server and access to your hosts file.

So if you are on Linux then edit your /etc/hosts file with the following:-

ip address of server www.test.com www.test.co.uk test.com test.co.uk www.nonsense.com

I am running the web server on my laptop so my hosts file looks like this

127.0.0.1 http://www.test.com http://www.test.co.uk test.com test.co.uk www.nonsense.eu

So now to edit the httpd.conf file, this was done with a yum install so the httpd.conf file is

vi /etc/httpd/conf/httpd.conf

Insert the following and try it out. I will explain more at the end 🙂

Note that some of the lines should not be used in production, more about this again at the end of the post.

 

<VirtualHost *:80 >
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /var/www/html/livesite/
ServerName www.test.co.uk
ErrorLog logs/test.com-error_log
CustomLog logs/test.com-access_log common
</VirtualHost>

<VirtualHost *:80 >
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /var/www/html/livesite/
ServerName test.co.uk
ErrorLog logs/test.com-error_log
CustomLog logs/test.com-access_log common
# Make the test.com into the www.test.co.uk
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test\.co.uk$ [NC]
RewriteRule (.*) http://www.test.co.uk$1 [L,R=301]
RewriteLogLevel 9
RewriteLog logs/test.co.uk-rewrite.log
</VirtualHost>

<VirtualHost *:80 >
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /var/www/html/livesite/
ServerName www.test.com
ErrorLog logs/test.com-error_log
CustomLog logs/test.com-access_log common
RewriteEngine on
# Make the .com into the .co.uk address
RewriteCond %{HTTP_HOST} ^(www\.)?test\.com$ [NC]
RewriteRule (.*) http://www.test.co.uk$1 [L,R=301]
# Make the test.com into the www.test.co.uk
RewriteCond %{HTTP_HOST} ^test\.com$ [NC]
RewriteRule (.*) http://www.test.co.uk$1 [L,R=301]
RewriteLogLevel 9
RewriteLog logs/test.com-rewrite.log
</VirtualHost>

<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /var/www/doone/
ServerAlias *
ErrorLog logs/nottest.com-error_log
CustomLog logs/nottest.com-access_log common
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)?test\.co.uk$ [NC]
RewriteRule (.*) http://www.google.com$1 [L,R=301]
RewriteLogLevel 9
RewriteLog logs/catchall-rewrite.log
</VirtualHost>

You will also need to run the following just to set you the test directories

 

mkdir /var/www/html/livesite
mkdir /var/www/html/livesite/test
mkdir /var/www/doone
echo <html> >>/var/www/html/livesite/index.html
echo <body> >>/var/www/html/livesite/index.html
echo It works >>/var/www/html/livesite/index.html
echo </body> >>/var/www/html/livesite/index.html
echo </html> >>/var/www/html/livesite/index.html 
echo <html> >>/var/www/index.html
echo <body> >>/var/www/index.html
echo Do One >>/var/www/index.html
echo </body> >>/var/www/index.html
echo </html> >>/var/www/index.html

 

OK now try it out,

 

You should be redirected to www.test.co.uk if you type www.test.com / test.com / test.co.uk / www.test.co.uk

If you enter though www.nonsense.eu, you should be redirected to www.google.com

The order of the virtual hosts was important in getting this correct, also note that this does not include the symlinks and other goodies that are contained usually with in the httpd.conf file. Again this is only to demonstrate how to get this up an running

Okay now for a bit of direction

All standard in bold as per normal installs

<VirtualHost *:80 >

ServerAdmin webmaster@dummy-host.example.com

DocumentRoot /var/www/html/livesite/

ServerName test.co.uk

ErrorLog logs/test.com-error_log

CustomLog logs/test.com-access_log common

Now here is some of the clever stuff.

# Make the test.com into the http://www.test.co.uk

You need to turn the engine on

RewriteEngine on

This looks out for test.co,uk

RewriteCond %{HTTP_HOST} ^test\.co.uk$ [NC]

This converts it into http://www.test.co.uk

RewriteRule (.*) http://www.test.co.uk$1 [L,R=301]

As you would have expected there was a lot of debugging required as I was just starting out with this, so to enable debugging you turn it on and then specify where you want the log file, this should be turned of when using it in a production server

RewriteLogLevel 9

RewriteLog logs/test.co.uk-rewrite.log

</VirtualHost>

 

Ok the next two virtualhosts are the same, the last one is a catch all, and in this circumstance will through you out to a different web address completely. In this circumstance just through them out to Google or a static html holding page, something that does not require much processing.

 

Again standard stuff in bold

<VirtualHost *:80>

ServerAdmin webmaster@dummy-host.example.com

DocumentRoot /var/www/doone/

ServerAlias *

ErrorLog logs/nottest.com-error_log

CustomLog logs/nottest.com-access_log common

This is the clever bit, not the ! In the front of the string being inspected

RewriteEngine on

So if the URL is not www.test.co.uk fire the rule

RewriteCond %{HTTP_HOST} !^(www\.)?test\.co.uk$ [NC]

Bounce the request out to Google

RewriteRule (.*) http://www.google.com$1 [L,R=301]

Again logging was switched on to see what was happening

RewriteLogLevel 9

RewriteLog logs/catchall-rewrite.log

</VirtualHost>

 

That’s all folks as bugs used to say :). Check back as I suspect there will be more to come when this gets put live.

Advertisements

One Comment on “Apache Rewrite rules how to fix .com to .co.uk URL’s”

  1. Natalie says:

    Thought this was a great article, i am used to doing everything inside PHP but the way you put it, once you know how its much easier with .htaccess. And proably more powerful too.

    Thanks loads.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s