homepage Welcome to WebmasterWorld Guest from 54.145.172.149
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
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

5+ Year Member



 
Msg#: 3878673 posted 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

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3878673 posted 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

5+ Year Member



 
Msg#: 3878673 posted 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

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3878673 posted 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

5+ Year Member



 
Msg#: 3878673 posted 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

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3878673 posted 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

5+ Year Member



 
Msg#: 3878673 posted 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

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3878673 posted 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

5+ Year Member



 
Msg#: 3878673 posted 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

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3878673 posted 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

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



 
Msg#: 3878673 posted 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

5+ Year Member



 
Msg#: 3878673 posted 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

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



 
Msg#: 3878673 posted 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

5+ Year Member



 
Msg#: 3878673 posted 1:36 am on Mar 26, 2009 (gmt 0)

Thanks Jim, it's working well now.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3878673 posted 3:06 am on Mar 26, 2009 (gmt 0)

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

jdMorgan

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



 
Msg#: 3878673 posted 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