homepage Welcome to WebmasterWorld Guest from 54.225.1.70
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Get ID From Friendly URL
matthewamzn




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

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




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

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




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

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




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

Yes, that is exactly what you need to do.

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

matthewamzn




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

'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




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

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

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

matthewamzn




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

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




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

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

matthewamzn




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

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




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

Yes, that's one version of it.

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

jdMorgan




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

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




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

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




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

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




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

Thanks Jim, it's working well now.

g1smd




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

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

jdMorgan




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

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

Jim

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved