PHP page rewrites done, now what does it look like?

Discuss practical ways rearrange URLs using mod_rewrite.

Postby richardk » Thu Oct 02, 2008 3:20 am

There is still a missing " at the end of the link
Code: Select all
<a href="productsincategories-ban-467-cat-1904235.html>

should be
Code: Select all
<a href="/productsincategories-ban-467-cat-1904235.html">


Code: Select all
Options +FollowSymLinks -MultiViews

RewriteEngine On

RewriteRule ^categories_and_featured_products-pid-([0-9]+)\.html$                 /categories_and_featured_products.php?pid=$1           [QSA,L]
RewriteRule ^subcategories_and_featured_products-pid-([0-9]+)-cid-([0-9]+)\.html$ /subcategories_and_featured_products.php?pid=$1&cid=$2 [QSA,L]
RewriteRule ^productsincategories-ban-([0-9]+)-cat-([-a-zA-Z0-9]+)\.html$         /productsincategories.php?ban=$1&cat=$2                [QSA,L]
RewriteRule ^products-ban-([0-9]+)-cat-([-a-zA-Z0-9]+)\.html$                     /products.php?ban=$1&cat=$2                            [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby servantofgod » Sat Oct 04, 2008 12:10 pm

Is it possible for me to take product.php URLs and add the product name to the url as well? For example:
Code: Select all
http://www.whatsshopping.com/products-ban-500-cat-RS-71073-BLACK.php


The product is a Women's Ankle Boot, and this product name is also an entry in the database. The desired result would be:

Code: Select all
http://www.whatsshopping.com/products-ban-500-cat-womens-ankle-boot-RS-71073-BLACK.php
servantofgod
 
Posts: 22
Joined: Wed Oct 01, 2008 8:14 am
Location: Chicago

Postby servantofgod » Sun Oct 05, 2008 8:32 am

Or how about if I could add a / after products, then input the product name, add another / then let the rest be as it is:

So:
http://www.whatsshopping.com/products-b ... -A18-4.php

is a 4 gig mp4 player.

I want it to reflect the product's name, which is in the database:

http://www.whatsshopping.com/MP4 Player 4GB 1.8 Inch-Screen + Password Setting/products-ban-331-cat-CVAAL-A18-4.php

Of course there are characters in that url that would have to be replaced such as the + and the period, as well as the spaces would need (-)'s. Is this possible?


Following posting this I tried it. I took the rewrite code from my htaccess:


Code: Select all
RewriteRule ^products-ban-([0-9]+)-cat-(.*)\.php$                     /products.php?ban=$1&cat=$2


and change it to:

Code: Select all
RewriteRule ^(.*)/(products-ban-([0-9]+)-cat-(.*)\.php$                     /products.php?ban=$1&cat=$2


Then went to the php file and changed the href from:
Code: Select all
<a href="products-ban-<?PHP echo $prod["pSection"]; ?>-cat-<?PHP echo $prod["pID"]; ?>.php" >


to:

Code: Select all
<a href="<?PHP echo $prod["pName"]; ?>/products-ban-<?PHP echo $prod["pSection"]; ?>-cat-<?PHP echo $prod["pID"]; ?>.php" >


Now the link of course displayed the product name. But after clicking it, it went to an error not found page.

Do you know what I was doing wrong?

[/code]
servantofgod
 
Posts: 22
Joined: Wed Oct 01, 2008 8:14 am
Location: Chicago

Postby richardk » Sun Oct 05, 2008 11:17 am

For /productname/products-ban-nnn-cat-zzz.html
Code: Select all
^products-ban-([0-9]+)-cat-([-a-zA-Z0-9]+)\.html$

would be replaced with
Code: Select all
^.+/products-ban-([0-9]+)-cat-([-a-zA-Z0-9]+)\.html$
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby servantofgod » Sun Oct 05, 2008 11:26 am

would that work to where I can have any characters in the product name?

Hmmmm. Dependant upon the product name (some products have characters like: "&, . , " and so on. Once you click the link the next page is severely distorted.
Last edited by servantofgod on Sun Oct 05, 2008 11:35 am, edited 1 time in total.
servantofgod
 
Posts: 22
Joined: Wed Oct 01, 2008 8:14 am
Location: Chicago

Postby richardk » Sun Oct 05, 2008 11:31 am

It should. They might get URL encoded (for example spaces becoming %20). You can do whatever you want to clean up the product names, like removing special characters, because it is ignored by mod_rewrite and PHP.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby servantofgod » Sun Oct 05, 2008 12:07 pm

Thanks!
servantofgod
 
Posts: 22
Joined: Wed Oct 01, 2008 8:14 am
Location: Chicago

Postby servantofgod » Sun Oct 05, 2008 8:02 pm

Hey also I noticed that some products have a / in them. Sometimes it is necessary to show say the mass of an object (eg: 1/8 oz) How can I get it to accept the dash without going to a distorted location. It seems to actually be following the dash as if it is another directory.
servantofgod
 
Posts: 22
Joined: Wed Oct 01, 2008 8:14 am
Location: Chicago

Postby servantofgod » Mon Oct 06, 2008 6:00 am

Actually can we translate that backslash into something else? like a dash
servantofgod
 
Posts: 22
Joined: Wed Oct 01, 2008 8:14 am
Location: Chicago

Postby richardk » Mon Oct 06, 2008 11:28 am

How can I get it to accept the dash without going to a distorted location.

Do you mean slash instead of dash? You might want to look at Relative paths to images, JavaScript, CSS and other external/linked files are broken.

You can replace the slash in the RewriteRule pattern if you want
Code: Select all
^.+-products-ban-([0-9]+)-cat-([-a-zA-Z0-9]+)\.html$


You can also replace slashes in the product name if you want.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

PreviousNext

Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 23 guests

cron