just starting out with mod_rewrite

Discuss practical ways rearrange URLs using mod_rewrite.

Postby jdulberg » Fri Aug 24, 2001 12:04 pm

I am in the process of getting my host to switch mod_rewrite on for my server and I was wondering if anyone could help me set up my first rewrite?

Basically, I'm working on a website that uses php/mysql and it has a bunch of ? and & that I'd like to cut down or get rid of completely.

Here is a sample current url:

domain.com/products.php?sid=molds&cid=5&id=10&page=1

sid = section
cid = category
id = item
page = current page

I'd love to cut the url down as much as possible. ie:

domain.com/products.php/molds/5/10/1 or less is its possible.

I'm really new to this so if anyone has an idea of how I can make the url look nicer, please let me know.

Also, what do I have to do to the php files themselves to get this type of thing working?

Thanks in advance:)

Jason.

<font size=-1>[ This Message was edited by: jdulberg on 2001-08-24 16:05 ]</font>
jdulberg
 
Posts: 3
Joined: Thu Aug 23, 2001 4:00 pm

Postby Brett » Fri Aug 24, 2001 8:19 pm

Hi Jason,

What you're trying to do is very similar to what I did when I set up this forum! See this thread.

I think the following will work:
Code: Select all
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/products.php/([a-z]*)/([0-9]*)/([0-9]*)/([0-9]*)
RewriteRule ^(.*) products.php?sid=$1&cid=$2&id=$3&page=$4   [L]
I don't think you need to do anything special with the script itself ... but on the other hand, PHP offers many different ways to access the variables defined in a query string, and I haven't tested them all.

Actually, if you want, you could accomplish this without using mod_rewrite at all ... by simply rewriting your php script to parse the $REQUEST_URI to determine the values of $sid, $cid, $id, and $page.
Brett
 
Posts: 82
Joined: Tue Jul 10, 2001 4:00 pm
Location: yohost.com

Postby jdulberg » Sat Aug 25, 2001 8:43 am

Thanks for your quick response!

It seems that HostPro doesn't support mod_rewrite even though its in the libexec directory. They said that I can add it myself if I want as mod_rewrite.so is in the libexec directory.

In the httpd.conf, do I just put

LoadModule rewrite_module libexec/mod_rewrite.so

and

AddModule mod_rewrite.c

Assuming I can get that working... would you recommend using mod_rewrite to accomplish what I'm aiming at doing or is the $request_uri a better way to go? I am not really sure how I go about using the $request_uri method.

Thank you for your time.

Jason.
jdulberg
 
Posts: 3
Joined: Thu Aug 23, 2001 4:00 pm

Postby Brett » Sat Aug 25, 2001 8:10 pm

To use the PHP/$REQUEST_URI method, you would use the ereg or preg_match function. You don't have to do anything to make your "revised" URL's point to your PHP script; they will work automatically.

Sorry, I can't advise about how to install mod_rewrite on your HostPro account.
Brett
 
Posts: 82
Joined: Tue Jul 10, 2001 4:00 pm
Location: yohost.com

Postby jdulberg » Sun Aug 26, 2001 12:39 pm

Cool, I got it working using the $PATH_INFO method.

Here's an example url now...
domain.com/products/molds/5/10-1

I forced "products" to be parsed as a php file so I could drop the .php extension.

Thanks.

Jason.
jdulberg
 
Posts: 3
Joined: Thu Aug 23, 2001 4:00 pm


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 1 guest

cron