Forum Moderators: phranque

Message Too Old, No Replies

.htaccess from static to dynamic mod_rewrite

Your help with this would be a great Xmas present!

         

mangotude

8:52 pm on Dec 6, 2003 (gmt 0)

10+ Year Member



Hi everyone,

I'm sure somebody smarter than me can help me with my problem.

I want to make available a certain amount of my pages on my website for search engines. I do not want them all to be available, so I will personally choose the pages and compile the static links in one page - eg profiles.html

I would like to set out the chosen links as follows:

STATIC
www.mysite.com/profile_5.html

on accessing this page, I want it to go to

DYNAMIC
www.mysite.com/index.php?page=view_profile&id=5

I think the only variable is the id number of the profile.

I have tried to use mod_rewrite with .htaccess - i know the code below isn't right, but i don't know what would have to be changed.

RewriteEngine on
RewriteRule ^profile_(.*).html index.php?page=view_profile&id=$1

Any assistance on this particular matter would be greatly appreciated.

jdMorgan

9:37 pm on Dec 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mangotude,

Welcome to WebmasterWorld [webmasterworld.com]!

> i know the code below isn't right

Your code is not perfect, but it should work. Why do you think it isn't right?

Jim

mangotude

10:09 pm on Dec 6, 2003 (gmt 0)

10+ Year Member



Hi Jim,

I went with it and it worked great. I had tried it earlier, but I must have had a typo. Thanks for your help.

James

jdMorgan

11:26 pm on Dec 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mangotude,

Glad to hear it worked. I didn't understand why there might be a problem...

Tweaking it just a bit:


RewriteRule ^profile_(.*)\.html$ /index.php?page=view_profile&id=$1 [L]

Escaping the period in front of 'html' makes the pattern more precise (because otherwise '.' means 'any character' in a mod_rewrite pattern), and adding an end anchor ('$') also makes the pattern more precise. Adding the [L] flag stops the rewrite engine after this URL is rewritten -- usually that's what you want, and it speeds things up slightly. Preceding the 'index.php' with a slash makes it easier for the server to resolve the new address, which is again very slightly faster.

Jim