Jump to content


Photo

PHP Generated Title -> Custom URL


Best Answer xProteuSx, 10 February 2014 - 12:48 AM

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.
Go to the full post


  • Please log in to reply
11 replies to this topic

#1 xProteuSx

xProteuSx

    Advanced Member

  • Members
  • PipPipPip
  • 450 posts
  • LocationVernon, Canada

Posted 26 November 2013 - 07:25 AM

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...m.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/...DALE-BANK-PLC-/

 

Except I would like a url like this:

 

http://www.mywebsite...e-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!



#2 Ch0cu3r

Ch0cu3r

    Advanced Member

  • Moderators
  • 1,698 posts

Posted 26 November 2013 - 08:14 AM

Yes it is possible.

 

I have a look at this thread. I helped someone with a similar question to yours.

http://forums.phpfre...-by-title-only/



#3 xProteuSx

xProteuSx

    Advanced Member

  • Members
  • PipPipPip
  • 450 posts
  • LocationVernon, Canada

Posted 27 November 2013 - 04:18 AM

@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?


#4 Ch0cu3r

Ch0cu3r

    Advanced Member

  • Moderators
  • 1,698 posts

Posted 27 November 2013 - 07:21 AM

The rewrite will only match urls like 

 

site.com/<id-here>-<page-title-here>

 

Your urls appear to be the reverse of that format



#5 xProteuSx

xProteuSx

    Advanced Member

  • Members
  • PipPipPip
  • 450 posts
  • LocationVernon, Canada

Posted 28 November 2013 - 02:30 AM

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.



#6 Ch0cu3r

Ch0cu3r

    Advanced Member

  • Moderators
  • 1,698 posts

Posted 28 November 2013 - 05:32 AM

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, 28 November 2013 - 05:33 AM.


#7 xProteuSx

xProteuSx

    Advanced Member

  • Members
  • PipPipPip
  • 450 posts
  • LocationVernon, Canada

Posted 29 November 2013 - 12:29 AM

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.



#8 xProteuSx

xProteuSx

    Advanced Member

  • Members
  • PipPipPip
  • 450 posts
  • LocationVernon, Canada

Posted 11 December 2013 - 06:25 AM

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, 11 December 2013 - 06:33 AM.


#9 xProteuSx

xProteuSx

    Advanced Member

  • Members
  • PipPipPip
  • 450 posts
  • LocationVernon, Canada

Posted 11 December 2013 - 06:52 AM

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).



#10 Ch0cu3r

Ch0cu3r

    Advanced Member

  • Moderators
  • 1,698 posts

Posted 11 December 2013 - 07:09 AM

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, 11 December 2013 - 07:10 AM.


#11 xProteuSx

xProteuSx

    Advanced Member

  • Members
  • PipPipPip
  • 450 posts
  • LocationVernon, Canada

Posted 10 February 2014 - 12:06 AM

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.


#12 xProteuSx

xProteuSx

    Advanced Member

  • Members
  • PipPipPip
  • 450 posts
  • LocationVernon, Canada

Posted 10 February 2014 - 12:48 AM   Best Answer

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.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com