Need help with setting up the url rewrite

New to mod_rewrite? This is a good place to start.

Need help with setting up the url rewrite

Postby revo » Sat Feb 14, 2009 8:01 am

I am totally new to mod_rewrite so if you could please be thourough so I know what to do - thank you for all the help you can give

Currently when someone views a detail view of a stores page on my site the url says:

...mysite.com/stores.php?storeid=34

and so this would be all of walmarts deals

I would like it to say:

...mysite.com/stores/walmart.htm

or even better

...mysite.com/deals/walmart.htm

Is this possible? storeid =34 is one table and in mysql tables it is related with walmart, but my site will request and pass the storeid numbers.

Thanks for any help
revo
 
Posts: 34
Joined: Fri Feb 13, 2009 2:02 pm

Postby richardk » Sun Feb 15, 2009 11:21 am

FAQ: How to change a numeric ID into a name/title. Post back here if you have any questions.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby revo » Wed Feb 18, 2009 8:30 am

Hi thanks for the quick response and great stuff as I am reading and learning.

I have:
mysite.com/stores.php?storeid=34
and the simplest way after reading all 4 options seems to me #3.
Will option #3 allow it to say the store name like:

mysite.com/deals/walmart.htm

I know option 1 there you gave us a way to do it with rewrite map, but I am waiting on the hosting co. to tell me if I have access to it. With #1 as we add stores we also have to add them to this id2name list right?

for option #3 is this correct to put this in .htaccess and thats it?

Options +FollowSymLinks

RewriteEngine On

RewriteRule ^deals/([0-9]+)-[^/]+\.htm$ /stores.php?id=$1 [QSA,L]

will this show in the url as:
mysite.com/deals/walmart.htm
OR
mysite.com/deals/34.htm

Thank you for reading
revo
 
Posts: 34
Joined: Fri Feb 13, 2009 2:02 pm

Postby richardk » Wed Feb 18, 2009 11:10 am

With #1 as we add stores we also have to add them to this id2name list right?

Yes but it would be a simple text file and you could probably automate it by editing your script where you add stores. (Editing and deleting would be harder but not impossible.)

for option #3 is this correct to put this in .htaccess and thats it?

Options +FollowSymLinks

RewriteEngine On

RewriteRule ^deals/([0-9]+)-[^/]+\.htm$ /stores.php?id=$1 [QSA,L]

No, option #3 is
3. Edit the script to use the name/title instead of the numeric ID. Then you will just need a normal RewriteRule
Code: Select all
RewriteRule ^files/([^/]+)\.html$ /file.php?name/title=$1 [QSA,L]

So you would want
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^deals/([^/]+)\.htm$ /stores.php?name=$1 [QSA,L]


will this show in the url as:
mysite.com/deals/walmart.htm

Mod_rewrite won't change the URLs in your pages. It will allow you to go to /deals/walmart.htm (you will be sent to /stores.php?name=walmart).
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby revo » Wed Feb 18, 2009 11:28 am

Thank you for your help, but I think I have confused matters for myself more. Can you tell me which of the 4 options will allow this in the url. I would like it to be similar to what many of coupon codes websites are using which is:

mysite.com/deals/walmart.htm

it may help with indexing, and visitors remembering the urls.

Would I have to change the table query from my db instead of using mysite.com/stores.php?storeid=34

to using

mysite.com/stores.php?storeid=walmart

what would you recommend from your expertise, and thank you very much for being simple and detailed where I actually understand things :-? I guess thats a wierd way to say it.
revo
 
Posts: 34
Joined: Fri Feb 13, 2009 2:02 pm

Postby richardk » Wed Feb 18, 2009 11:42 am

Can you tell me which of the 4 options will allow this in the url.

1, 3, 4 (and 5).

Would I have to change the table query from my db instead of using mysite.com/stores.php?storeid=34

to using

mysite.com/stores.php?storeid=walmart

For #3, yes. And for option #4 it would be a new query in the "name to ID" script.

what would you recommend from your expertise

#3 is the best option.

You also have to edit how the links are outputted by your script.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby revo » Wed Feb 18, 2009 1:38 pm

Hi,

I just left out the www from these two sample sites:

ultimatecoupons.com/coupons/bestbuy_coupons.htm

couponcactus.com/coupons/Target/

I am sure they have something automatic that changes the dynamic url to a static looking one and thats what I am looking to setup. Even in the website when you hover over a link it shows it like above down at the bottom of the browser. Am I even looking into the right thing before I start? If yes, then use this:

Options +FollowSymLinks

RewriteEngine On

RewriteRule ^deals/([^/]+)\.htm$ /stores.php?name=$1 [QSA,L]

and what would I have to setup to edit how my links are outputted as you advised me.

If this is all wrong for how I would like to set it up, please tell me what to do.

Thank you very much as I look to you for help!
revo
 
Posts: 34
Joined: Fri Feb 13, 2009 2:02 pm

Postby richardk » Thu Feb 19, 2009 6:55 am

I am sure they have something automatic that changes the dynamic url to a static looking one and thats what I am looking to setup. Even in the website when you hover over a link it shows it like above down at the bottom of the browser. Am I even looking into the right thing before I start?

For all you know they could have HTML files and directories set up. Mod_rewrite can help you achieve that type of URL, though.

If yes, then use this:
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^deals/([^/]+)\.htm$ /stores.php?name=$1 [QSA,L]

Yes you would use that mod_rewrite.

what would I have to setup to edit how my links are outputted as you advised me.

You would open the PHP files, find where the links are outputted (search for href=) and change how they are outputted to include the name instead of the ID. Then you would change the SQL query in stores.php to use the name instead of the ID when searching the database (and the $_GET['name'] variable instead of $_GET['id']).
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

setting the ID and store field

Postby revo » Thu Jun 11, 2009 10:40 am

I am trying to do the modrewrite as you described in the last message w/o using the httpconf file b/c my host does not give access to that right now unless I upgrade and I may have to to make this easier if you suggest.

I changed the href= to the store name instead of the ID, then in my receiving page I changed
$_GET['store'] variable instead of $_GET['ID']).

but it does not show any stores, I think because I have

FROM (`deals` join `sellers` on((`deals`.`ID` = `sellers`.`ID`)))
WHERE sellers.ID = columname

my columname is equal to the $_GET['info'] variable and this comes from the link the visitor clicks.

the text link is href="modes.php?info=<?php echo $row_rsdeals['store']; ?>

I know this is database stuff, but I am stuck in between here, can you suggest anything please?

My novice thought::
In my sql database- how can I make "store" equal "ID" in the same table, so that then "ID" equals "ID" in a different table?
setting two columns equal in the same table like store = id so that sears=322
revo
 
Posts: 34
Joined: Fri Feb 13, 2009 2:02 pm

Postby richardk » Thu Jun 11, 2009 10:50 am

You probably only need to add a new column to the stores database table with the store's name in it (with special characters, eg. spaces, replaced or removed. This is usually called a stub and should be what you use in the URL.) The change your query to check the store name/stub instead of the ID.
Code: Select all
WHERE sellers.StoreName = store_name_from_the_GET_array

instead of
Code: Select all
WHERE sellers.ID = columname
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Next

Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 5 guests

cron