Wondering how to do this with mod_rewrite?

Currently I have these type of URLs…


I’m wondering if I can use mod_rewrite to make it as friendly as possible?

Perhaps something like


Thanks in advance.


First, WELCOME to SitePoint’s Apache forum!

To answer your question, of course! The only trick is for you to define exactly what you want your redirects to do. I’ve several (4? 5? 6? more?) years of experience in this forum helping members to LEARN mod_rewrite so I’ve attempted to save my carpal tunnels by accumulating that knowledge in a tutorial. IMHO, you can get some good ideas (and example code) how to do this as well as what others members have done through the years. That tutorial is linked in my signature (it was published in a slightly truncated version by SitePoint but I can only keep my page up to date).



That’s all a bit above my head. My IT contractor is basically telling me it’s going to take 16 hours to do and is quoting me close to $3000 to do it.

I basically need to know a rough guide of what to tell them to do so I sound like I know what Im talking about so they dont rip me off…

So roughly I need something like…

RewriteEngine on
RewriteRule ^/?([a-zA-Z_]+)$
database/blah/mainPage.html?siteID=$1 [L]


Ok that I get. But what if I want it to be domain.com/siteID/mainPage.html and have it change to productPage.html when I click that link?

Like I said, a little over my head :wink:


The first thing you need to do is identify the format of the links you’re using in your scripts.

Second, identify the format of the links you want your visitors to see (being sure to keep the information from #1 available).

If you can put those to words and identify the characters involved in each, then it’s a trivial matter to write the mod_rewrite statements to convert one to the other.

One noobie falacy, though, is that mod_rewrite converts your links TO the new format. Although it can, that would make your code “loopy” as Apache can’t serve the “nonsensical” links your visitor sees so the mod_rewrite’s real task is to convert that to something Apache can use to fetch the correct script. Have a read of the tutorial linked in my signature for how that’s developed - and sample code.

If it’s still over your head at that point, please tell me what the first two steps are so I can show you how to get from B back to A using mod_rewrite.