The UHF of the film world.


quietearth [General News 08.19.06]

Share on Google+


How to redirect all urls to be processed by one file in apache. This is based on the article at http://www.alistapart.com/articles/urls.

If you are coding your own blog, you will notice that many sites use the format:
http://somesite/blog/XXXX/XX/XX/article-name-here
or something similar for the permalinks. This is pretty convenient, especially if you are using a database backend. I wanted to redirect http://www.quietearth.us/articles and everything underneath it to be processed by one file. Using mod_rewrite we can do this pretty easily, I just created the articles subdirectory and added the following into .htaccess:


# this is /articles/.htaccess
Options       FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ /path/to/your/website/articles/index.php


Since the .htaccess is in the articles subdirectory, any url starting with /articles will then be processed by the specified index.php, and it does all this transparently to the user.

Now if you are using php, in the index.php we can then access the request url so we could look it up in a database like such:
$_SERVER['REQUEST_URI'];
This variable will give us the following as an example:
/articles/2006/08/18/some-article-here
which you can then parse.

Also important here is security, we must check all variables, especially REQUEST_URI before using it anywhere in our code.

If you are looking to get a bit fancier with processing, as mod_rewrite is capable of complex regular expressions, I suggest looking at the article I linked to up above for more detailed information or looking at the docs for mod_rewrite.


Leave a comment








Related articles