Welcome to WebmasterWorld Guest from 54.145.39.186

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Get ID From Friendly URL

     

matthewamzn

6:49 pm on Mar 25, 2009 (gmt 0)

10+ Year Member



I'm trying to make this more friendly url work. I need to get the id from the url to display the page:

[___.com...]

[___.com...]

So far in my .htaccess file I have:
RewriteEngine On
RewriteRule ^/([0-9]+)_([a-z]+)_([a-z]+)_([0-9]+).html$ /vehicle.php?id=$4

g1smd

7:25 pm on Mar 25, 2009 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



This is tricky in many ways, because you can inadvertently create infinite duplicate content.

If you discard the content of $1, $2 and $3, and only use the number in $4 as a key to pull the record from the database, I could link to www.example.com/useless_junk_product_26.html and the same page would display.

You really do need to take the other parts and validate them against the real article title as stored in the database - and then either send a 404 Not Found page, or redirect the user to the correct URL for that content.

RewriteRule cannot see the leading / on a URL request. That's the main reason your rule does not work.

Be aware that your rule will also respond to both www and non-www requests unless you take action to redirect all non-www URL requests over to the www version of the URL. This redirect must go before the rewrite in the .htaccess file.

matthewamzn

7:33 pm on Mar 25, 2009 (gmt 0)

10+ Year Member



Could I get all the values and than check them using php?

[___.com...]

[___.com...]

RewriteEngine On
RewriteRule ^/([0-9]+)_([a-z]+)_([a-z]+)_([0-9]+).html$ /vehicle.php?id=$4&year=$1&make=$2&model=$3

<?php some php code that checks to see if the year, make and model match the actual values for the id ?>

[edited by: matthewamzn at 7:36 pm (utc) on Mar. 25, 2009]

g1smd

7:35 pm on Mar 25, 2009 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Yes, that is exactly what you need to do.

Whether you then send HEADER 301 or HEADER 404 is up to you.

matthewamzn

7:39 pm on Mar 25, 2009 (gmt 0)

10+ Year Member



'RewriteRule cannot see the leading / on a URL request. That's the main reason your rule does not work.'

Do I need to make it see it or put something in it's place?

g1smd

8:31 pm on Mar 25, 2009 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



... Or take it out of your pattern. :)

There's no leading slash on what your pattern is attempting to match against.

matthewamzn

8:49 pm on Mar 25, 2009 (gmt 0)

10+ Year Member



ok thanks, this is working now:

RewriteEngine On
RewriteRule ^([0-9]+)_([a-z]+)_([a-z]+)_([0-9]+).html$ /vehicle.php?id=$4&year=$1&make=$2&model=$3

g1smd

10:40 pm on Mar 25, 2009 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Don't forget that extra non-www to www 301 redirect ahead of your rewrite!

matthewamzn

11:51 pm on Mar 25, 2009 (gmt 0)

10+ Year Member



Is that important for seo?

I looked it up. Do you mean something like this?

RewriteEngine On
RewriteCond %{HTTP_HOST} ^_______\.com [NC]
RewriteRule ^(.*) [_______.com...] [R=301,L]
RewriteRule ^([0-9]+)_([a-z]+)_([a-z]+)_([0-9]+).html$ /vehicle.php?id=$4&year=$1&make=$2&model=$3

g1smd

12:32 am on Mar 26, 2009 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Yes, that's one version of it.

Yes it is important, otherwise every page of your website ends up with two Duplicate URLs.

jdMorgan

1:02 am on Mar 26, 2009 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Escape all literal periods in patterns as shown, and add an [L] flag to your rewrite:

RewriteEngine On
#
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#
RewriteRule ^([0-9]+)_([a-z]+)_([a-z]+)_([0-9]+)\.html$ /vehicle.php?id=$4&year=$1&make=$2&model=$3 [L]

Jim

[edited by: jdMorgan at 1:11 am (utc) on Mar. 26, 2009]

matthewamzn

1:08 am on Mar 26, 2009 (gmt 0)

10+ Year Member



I'm having one problem with it. It's removing the directories if www. isn't included.

_____.com/path/page.php

becomes

[_____.com...]

Shouldn't the * tell it to append the entire path?

RewriteEngine On
RewriteCond %{HTTP_HOST} ^_____.com [NC]
RewriteRule ^(.*)$ [_____.com...] [R=301,L]
RewriteRule ^([0-9]+)_([a-z]+)_([a-z]+)_([0-9]+).html$ /vehicle.php?id=$4&year=$1&make=$2&model=$3

jdMorgan

1:13 am on Mar 26, 2009 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Yes, (.*) matches the entire URL-path into the local variable $1, so it is re-inserted in the substitution URL.

Be sure you completely flush your browser cache after changing any server-side code.

Jim

matthewamzn

1:36 am on Mar 26, 2009 (gmt 0)

10+ Year Member



Thanks Jim, it's working well now.

g1smd

3:06 am on Mar 26, 2009 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



You need [L] on the end of the final rewrite line.

jdMorgan

2:37 pm on Mar 26, 2009 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



And the other changes/improvements, as posted above...

Jim

 

Featured Threads

Hot Threads This Week

Hot Threads This Month