Jump to content

PHP Generated Title -> Custom URL


xProteuSx
Go to solution Solved by xProteuSx,

Recommended Posts

Hi folks ;)

 

I've got a bit of a whopper (I think), especially since I am really unfamiliar and uncomfortable with regex ...

 

I have a website with 46,000+ products.  For each product you get a URL that is something like this:

 

http://www.mywebsite.com/viewitem.html?id=12345

 

Also, because the items are all in a MySQL database, I have PHP generate a <title></title> for each and every product, so for the link above it would be something like this:

 

<title>Silly Shoes: Blue Laces Size 2</title>

 

I would absolutely LOVE to know how to do something like this:  MySQL Description -> PHP Title -> custom URL.

 

I am looking to do somethink like what eBay does:

 

http://www.ebay.com/itm/UK-SCOTLAND-10-Pounds-15-11-2007-CLYDESDALE-BANK-PLC-/

 

Except I would like a url like this:

 

http://www.mywebsite.com/shoes/silly-shoes-blue-laces-size-2/

 

Is it possible to go from MySQL to PHP to custom URL in one shot?  If so, how can I do this?

 

As always, thank you in advance, and CHeers!

Link to comment
Share on other sites

@Ch0cu3r

 

Thank you for your reply.  I have followed the instructions in your post (accurately, I think), but cannot achieve the desired result.  Here is what I've got:

 

 

.htaccess file:

 

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)-([a-z0-9_-]+)/?$ viewnote.html?id=$1&title=$2
 
 
Here is a sample of the formatting of my links:
 
<a href="viewnote.html?id=10000&title=armenia-p1-10-rubles-from-1919">Click Here</a>
 
When I click on the link I simply get:
 
mysite.com/viewnote.html?id=10000&title=armenia-p1-10-rubles-from-1919
 
Ofcourse, the desired result is:
 
mysite.com/armenia-p1-10-rubles-from-1919  (with the "id" being passed to that particular page so that the relevant content is displayed).
 
Where have I gone wrong?
Link to comment
Share on other sites

Actually, the way I am trying to do things, and the way I thought I had it set up was a link like this:

 

mysite.com/viewnote.html?id=10000&title=armenia-p1-10-rubles-from-1919

 

Would generate a URL like this:

 

mysite.com/armenia-p1-10-rubles-from-1919

 

But it would also send the following to that page:  $_GET["id"] = 10000;

 

The ID should not show up in the SEO Friendly URL at all ... its in the original URL just so that the page that is being linked to actually has this value.

Link to comment
Share on other sites

This rule here

RewriteRule ^([0-9]+)-([a-z0-9_-]+)/?$ viewnote.html?id=$1&title=$2

Will match this url

mysite.com/10000-armenia-p1-10-rubles-from-1919

                ^

                +-- first dash

 

       ([0-9]+) matches the numbers before the first dash and passes it as the product id ($1)

([a-z0-9_-]+)/? matches anything after the first dash and passes it as the product title ($2)

 

The rewrite rule will not match this url

mysite.com/armenia-p1-10-rubles-from-1919

Edited by Ch0cu3r
Link to comment
Share on other sites

Yay!  I finally got it.  Its probably so ridiculous that you guys never considered a human being who is smart enough to use a keyboard to be dumb enough to get this wrong:

 

I was using links like this:

 

mysite.com/viewnote.html?id=10000&title=armenia-p1-10-rubles-from-1919

 

Instead of using links like this:

 

mysite.com/10000-armenia-p1-10-rubles-from-1919

 

I was under the impression that you had to have the prior to produce the latter.  As it turns out, you skip that step and move right onto the latter link (SEO Friendly one).

 

Cheers, and thank you very much.

Link to comment
Share on other sites

  • 2 weeks later...

Hi guys! I am trying to expand on this mod_rewrite idea, and am trying to implement two rules.  However, it seems as though only the first is being implemented, even for the second example.  Here is what I have in my .htaccess file:

 

 

----------------------------------------------------------------------------

RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html
 
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)-([a-z0-9_-]+)/?$ viewnote.html?id=$1&title=$2
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)-([a-z]+)/?$ nation.html?id=$1&title=$2
----------------------------------------------------------------------------
 
Both link formats are the same.
 
The first re-write rule is used to display specific products within a segment of the gallery.
 
The second re-write rule is supposed to display a sub-segment of the gallery (in this case a country).
 
Both rules utilize a numerical id (each product and each country both have a numerical value associated with them) and a descriptive title.  How can I get mod_rewrite to differentiate between 'sub-segment of gallery' and 'specific product'??
 
At present, if I click on a link that is supposed to send me to a sub-gallery, it will send me to a specific product page if the id numbers for the product and the country are both the same, otherwise I get a MySQL error.
 
ie.  If the id number for the sub-gallery is 10, and a product with id number 10 exists then it sends me to product with id 10 page, instead of sub-gallery with id 10 page.
ie.  If the id number for the sub-gallery is 22, and there is no product with id number 22 then I get a MySQL error (because there is a MySQL query which requires a valid id).
 
Cheers.
Edited by xProteuSx
Link to comment
Share on other sites

Oh, I should mention that my links look like this:

 

www.mysite.com/82-paper-money-from-equatorial-african-states

 

Of course, this should display gallery number 82 (paper money from equatorial african states, which is sub-gallery number 82) but instead shows me product 82 on the page).

Link to comment
Share on other sites

You need to make the urls for products and and galleries unique, so mod_rewrite can differentiate between the two. I would set up urls to prefix the type of page you are accessing, for example

 

For galleries it would be

site.com/gallery/<gallery_id>-<gallery_title>/

 

For product it would be
site.com/product/<product_id>-<product_title>/

 

As you see having th gallery and product prefixed makes them distinguishable. You'd set the rewrite rules as

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^gallery/([0-9]+)-([a-z0-9_-]+)/?$ viewnote.html?id=$1&title=$2 [L,NC] # rwrite for gallery
RewriteRule ^product([0-9]+)-([a-z]+)/?$ nation.html?id=$1&title=$2 [L,NC]         # rewrite for product
Edited by Ch0cu3r
Link to comment
Share on other sites

  • 1 month later...

Man, I sure hope you guys can help me out with this one.  I have tried to apply the dual mod-rewrite rules, and only the first works.  However, each link is unique, so I don't see the problem.  Here is what I've got for the two rules:

 

RewriteRule ^country/([0-9]+)-([a-z]+)/?$ nation.html?id=$1&title=$2  [L,NC]       # rewrite for country
RewriteRule ^banknote/([0-9]+)-([a-z0-9_-]+)/?$ viewnote.html?id=$1&title=$2 [L,NC] # rwrite for banknote
 
So the links for the first rewrite rule look like this:
 
banknote/2634-cambodia-p5b-20-riels-from-1956
 
They work perfectly!
 
The links for the second rewrite rule look like this:
 
country/84-french-afars-and-issas
 
This second link takes me to a 404.
 
Also, I noticed something strange.  When one of the 'country' links involves a country that has a single-word name (ie.  Poland, Canada, Argentina) the link will take me to a 404, but the address bar will be the referring page.  So, for example, if I have the link:      country/2-france     on page somepage.html, I will get a 404 error on somepage.html.  However, if the 'country' link involves a country that has a multi-word name (ie.  Papua New Guinea, New Zealand, United States) the link will take me to a 404, but will reflect the initial link.  So, for example if I have the link:   country/12-new-zealand     on page somepage.html, I will get a 404 error on country/12-new-zealand.
 
Anyways, I can't continue working on my project until I get this resolved, as it has almost completely killed the functionality of the site.
 
Please help ASAP!!  Its urgent.  Thank you in advance.
Link to comment
Share on other sites

  • Solution

Found ANOTHER dumb move on my part.

 

nation.html was not accepting a variable named 'id'; it was looking for a variable named 'country'.

 

I simply changed the rewrite rule to this:

 

 
RewriteRule ^country/([0-9]+)-([a-z_-]+)/?$ nation.html?country=$1&title=$2  [L,NC]
 
Now everything is peachy!  Thanks guys.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.