Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

James Robertson http://www.r0bertson.co.uk

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Using Apache with RewriteMap and a text file

This code will read a text file using Apache then upon a user request, look for a url containing /l/ eg. http://mysite.com/l/digg and rewrite the URL in full eg. http://mysite.com/gwd/feed/digg.html . The example file is shown here for completeness.

RewriteMap uses a text file as a convenient alternative for the administrator when declaring many rewrite rules. Code based on the article from ONLamp.com - A Day in the Life of #Apache http://urltea.com/1vwb


<file name="links.txt" location="/var/www/localhost/">
scotsman /gwd/feed/scotsman.html
digg /gwd/feed/digg.html
</file>

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteMap links txt:/var/www/localhost/links.txt
RewriteRule ^/l/(.*) ${links:$1|http://mysite.com/} [R]
</IfModule>



Note: The [R] at the end of RewriteRule means redirect, to have a clean url simply remove that switch.

*update 15-Feb-08 *
Restart Apache rather reloading the module when switching redirection on or off for a RewriteRule. The Apache version 2.2.6 (Unix) didn't pick up my settings correctly when I tried /etc/init.d/apache2 reload, instead I needed to use /etc/init.d/apache2 restart.

A simple mod_rewrite example

Rewrite a long url as a short easy to remember url with mod_rewrite. Place this code in httpd.conf or the virtual hosts file. Original code from sitepoint - mod_rewrite: A Beginner's Guide to URL Rewriting http://urltea.com/1v6d .

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/shortcut$ /complicated/and/way/too/long/url/here
</IfModule>
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS