I'm a total noob to using mod_rewrite and I'm reading tutorials and trying to wrap my head around it but it's not quite coming together. What I need to accomplish is this:

take a bunch of urls structed this way: yoursite.com?index.php?id=#

and rewriting it so it becomes yoursite.com/whatever depending upon what the value of # is. So what I'm wondering is, is there a way to apply if statements or swtich statements using mod_rewrite or does it have similar syntax?

Recommended Answers

All 9 Replies

Member Avatar

what I'd do:

RewriteEngine On
RewriteRule ^([^/]*)$ /?index.php?id=$1 [L]

Wouldn't that rewrite it the opposite way? Turning yoursite.com/whatever to yoursite.com/index.php?id=# (still trying to get the hang of this)

Basically what I'm trying to do is this:

yoursite.com/index.php?id=1 rewrite to yoursite.com/products

yoursite.com/index.php?id=2 rewrite to yoursite.com/aboutus

etc for all of my url's. removing the index.php?id=# part and replacing with a descriptive word.

Another thing, like I said I'm totally new to this so I might be misunderstanding something. I was under the assumption that the rewriting part is sort of virtual? Like yoursite.com/products simply shows the content for yoursite.com/index.php?id=1 without the products directory having to physically exist. But all my testing leads to 404 errors. (I might just have this whole rewrting rules thing backwards or maybe something isn't enabled on the server)

Here's my test .htaccess file:

            RewriteEngine on
            RewriteRule ^test.html$ home [L]

But with this I get a 404 error that /home doesn't exist. So is my understand of how this works wrong, or something server side?

Wouldn't that rewrite it the opposite way?

Yes. That is basically how it works. What you type in the URL is /product/12 (this is your virtual page). The rewrite rules change this to product.php?id=12 (your actual page) which should be the script you have, with the parameters you need. The address bar will still show what you typed, and this is also what gets indexed by search engines.

You example is indeed the wrong way around. The virtual comes first, the actual second. So, if you want /home to show you'd need:

RewriteRule ^home$ test.html [L]

Thanks a lot :D Okay one last question and then I think I should be good to go.

Is there a way to work something like a switch statement in htaccess, so I wouldn't have to write a rule for each url? Like using variables or something?

    RewriteRule ^directoryName/?$ index.php?id=$1 [L]

where directoryName would be determined by the value of $1?

Just when I think I only have one last question, another pops up.

Okay so my understanding is now when I type in mysite.com/home it will display the content from test.html which is awesome. My question is if I had internal links in my site for test.html would they also get rewritten. Say I click on a link for test.html would it go to /test.html or /home? And if it doesn't go to /home, how do I handle that?

Yes. You use regular expressions.

RewriteRule ^directoryName/(.*)$ index.php?id=$1 [L]

No, what is pointing to test.html in your HTML you should change to home yourself. How? Depends on how you are doing this. I don't think there's a single answer for what you want. If these links are hard-coded, you'll just have to change them manually.

okay so, mod_rewrite can't be used to change existing links? If I have internal links to say, index.php?id=1 in my navigation that would need to be changed to /products (or whatever) in the actual html files? I've been asked to research into how this all works in an effort to rewrite dynamic urls to something a little more SEO friendly. But it's all new to be so there's a bit of fumbling on my part :P

As for regular expressions, I haven't worked with them before, I've been reading up on them since I started looking into the mod_rewrite stuff. Which, clearly I'm going to have to look into a little more in depth because I'm not totally understanding what you mean there :D

Have a look at regular-expressions.info, it contains some nice information.

Thanks a lot :D I think we can considering this part of my enquiry solved. Yay, onto the hard stuff XD

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.