Help: Mod_Rewrite


I am new to Apache’s mod_rewrite.

I am trying to make my urls more friendly, in which I have some url like:


How do I write the RewriteRule (and RewriteCond?) on my .htaccess which will function in this way:

both /dir1/ and /dir1 point to /content.php?directory=dir1


both /dir1/page1/ and /dir1/page1 point to /content.php?directory=dir1&page=pg1


/ (ie, point to /content.php?directory=home

both dir1 and pg1 are variables.

I need some helps from you guys.

Thank you.


Without knowing the backend you’re using to run your site I can’t guarantee this will work but something like:

RewriteEngine On
RewriteBase /
RewriteRule ^p([0-9]+)/dir$ /content.php?directory=dir$1 [QSA]
RewriteRule ^p([0-9]+)/dir$/page$ /content.php?directory=dir$1&page=pg$2 [QSA]

Should at least give you somewhere to start from.