Sunday, May 20, 2012
 

Nice url concept with mod_rewrite

This time i will share some simple examples how to make nice url for any application. It should be adopted for any server Apache or lighttpd

So there are two ways of storing nice url.

  1. Storing nice url path in database
  2. Do not store nice url data in database, but instead of it, make url parsable by mod_rewrite.

First solution

Talking about first solution it requires additional overhead by processing request and making query to database to get real destination. For example

/very-nice-url/article-name

Actually would go to:

/article/view/15

Also it have and some advantages, perfectly clean url.

Second solution

Personally i prefer second solution.

Request url would be like.

/article-name-15a.html

Actually it would go to

/article/view/15

But this time it does not require database, everything is done by mod_rewrite.

Mod_rewrite rules

For apache it might look like: 

RewriteEngine On
RewriteRule ^(.*)-([0-9]*)a.html$ index.php/article/view/$2
!\.(gif|jpe?g|png|css|js|html|swf|php)|var(.+)storage.pdf(.+)\.pdf$ index.php
DirectoryIndex index.php

For lighttpd it might look like:

url.rewrite-once = (
"^/(.*)-([0-9]*)(a\.html)(.*?)$" =>  "/index.php/article/view/$2$4",
"^/.*\.(css|html|swf|htm|pdf|txt|js|tgz|png|zip|gif|ico|jpe?g)$" => "$0",
"^/(.+)/?$" => "/index.php/$1"
)

Conclusions

What solution to use depends on your needs. Personally i prefer second solution it's faster and does not require database processing and influence to url is minimal.

Back »

Comments: 0

Leave a reply »

 
  • Leave a Reply
    Your gravatar
    Your Name
     
     
     
     
 
About Remdex site

Simple site for simple peoples.

Get in touch

E-mail: remdex@gmail.com