Home     Articles & Projects     Products & Web Services     Forum

.htaccess Rewrites

Hi David,

So pleased you have opened this forum up - I have soooo many questions and am so hungry for knowledge!

I am using Magic parser to build a site from the Laterooms API, all is going well but have a question regarding .htaccess rewrites.
I believe that the best URL's are short and with that in mind I am trying to construct the URL's

How do you pass information in a URL, but not necessarily use all the information in the rewrite. For example....

hotel/".$results["REGION_CODE"]."-".$results["REGION_DESCRIPTION"]."-".$results["TOWN_CODE"]."-".$results["TOWN_DESCRIPTION"]."

The above URL will pass through 4 variables. I know how to rewrite using all 4, but is it possible to use one variable for the rewrite but still pass the others through for use in the script.

RewriteRule ^hotel/(.*)-(.*)-(.*)-(.*).html$ hotel_results.php?region_code=$1&region_description=$2&town_code=$3&town_description=$4 [NC]

I would like the URL to look like this, but still have the other 3 variables available to use. Does this make sense?
RewriteRule ^hotel/(.*).html$ hotel_results.php?town_description=$1 [NC]

Also is it best to end a rewritten URL with / or .html?

Thanks,
Simon

Hi Simon, Last question

Hi Simon,

Last question first - I always end my re-written URLs in .html. I think it makes for a very neat, professional looking site; and doesn't confuse browsers or (more importantly) search engine spiders at all - they know exactly how to deal with it.

Now, regarding the URL, my preference is actually (if this is an option) to work entirely on ID values (which you encode into the URL however you prefer based on your SEO assumptions) together with an ID field. However, you also have the opportunity within these sort of URLs to create a very search engine friendly hierarchy, where URLs that do not delve down as deep as an individual hotel generate an index at the current level. For example:

/hotel/(Region-Code)Region-Description/

e.g. /hotel/(1234)England/

At this level you have a directory of towns within that region. Followed by:

/hotel/(Region-Code)Region-Description/(Town-Code)Town-Description/

e.g. /hotel/(1234)England/(5678)Stafford/

At this level you have an index of all hotels in Stafford, England (The Swan is a good one ;)

Finally,

/hotel/(Region-Code)Region-Description/(Town-Code)Town-Description/hotel-id.html

Is an individual hotel page, e.g.

/hotel/(1234)England/(5678)Stafford/9876.html

Although you might also want to include the hotel name, possibly good for SEO purposes:

/hotel/(1234)England/(5678)Stafford/9876(The-Swan).html

I always use ID values from a URL rather than relying on text; which means that you can sanitize the text in the same way that Price Tapestry strips all URL dangerous characters from the product name during import; and not have to worry about it exactly matching what is in the database.

[if you choose to go down this sort of route, it looks like you know how to construct the rewrite rules, but let me know if you need help with that also]

Such a function might look like this (much preferred to just using urlencode):

<?php
 
function urlsafe($text)
  {   
   
$text = preg_replace('/[^A-Za-z0-9' ]/e','',$text);
    $text = str_replace("-"," ",$text);
    return trim($text);
  }
?>

Then, you would use this function when constructing URLs throughout your site, for example:

<?php
    $url
= "hotel/(".$results["REGION_CODE"].")".urlsafe($results["REGION_DESCRIPTION"])."/(".$results["TOWN_CODE"].")".urlsafe($results["TOWN_DESCRIPTION"]."/";
?>

Hope this helps!
Cheers,
David.

Thanks David, I currently

Thanks David,

I currently have my URL's rewritten as you suggested above but didn't realise that it was good practice to create a hierarchy. I have always thought that it was best practice to keep URL's as short as possible!

Thanks again for your help.
Regards,
Simon.

Hi David, Just a quick debug

Hi David,

Just a quick debug of your last script you posted.

  function urlsafe($text)
  {  
    $text = preg_replace('/[^A-Za-z0-9 ]/e','',$text);
    $text = str_replace(" ","-",$text);
    return trim($text);
  }

The URL rewrites are fine but a name like "Dumfries and Galloway" seems to confuse the rewrite process with the hyphens added in the urlsafe function and as a result doesn't work.

http://www.scottishhotels.net/test/county/11541-Dumfries-and-Galloway/

Any ideas what it could be?

Thanks,
Simon

Hi Simon, (this is in

Hi Simon,

(this is in response to your post before last!)

I think a lot depends on your personal SEO assumptions (if that is the objective). Personally, with content sites (especially big content sites) I have found that you can get very good AdSense targetting through the keywords being in the URL alone. This is because the page URL is (i'm sure) one of the parameters in the AdSense invocation request.

The other strategy I take with a hierarchical structure is then to seek deep, relevant inbound links. So, for example, I would seek a link from a Stafford related site directly into /hotels/England/Stafford/, which, given enough inbound PR, search engines may treat as a site in its own right; allocating as much resource to indexing that directory and its sub-directories as it's "quality" demands.

Cheers,
David.

The rewrite works if I

The rewrite works if I change it to

http://www.scottishhotels.net/test/county/11541/Dumfries-and-Galloway/

Seems a bit strange?
Simon

Hi Simon, It will be the

Hi Simon,

It will be the hypens in the name confusing the rule. As you are splitting on the / character in the second example it's not a problem. To use brackets as the same level, I think they need to be escaped in the expression, for example:

RewriteRule ^hotel/\((.*)\)(.*)/$ hotel_results.php?region_code=$1

Cheers,
David.

Hi David, Considering the

Hi David,

Considering the above comments, why did you choose to end the cataegory URL's etc. with / in the PT script and not .html?

http://www.webpricecheck.co.uk/category/

Regards,
Simon.

Hi Simon, The products

Hi Simon,

The products themselves do end in .html - as this indicates "you have reached the bottom". The higher level folders, ending in "/" indicate that an index exists at that level.... sorry should have made that clearer before..!

Cheers,
David.

Hi David, I have read this

Hi David,

I have read this thread over and over again and still cannot figure out how to send 3 variables in a url but only show 2 in the re-written url?

Just to clarify, I am trying to send this url

www.gynogapod.co.uk/productView.php?prod_ean=1111111111&prod_name=qwerty&pid=123

to be re-written like this... i.e. with showing the pid value
www.gynogapod.co.uk/1111111111-qwerty.html

This is what I think it should look like
RewriteRule ^(.*)-(.*).html$ productView.php?name=$1&pid=$2&%{QUERY_STRING} [L]

coming from this...
echo "<td align=\"left\"><a href=\"/".$row['prod_ean']."-".urlsafe($row['prod_name'])."-".$row['prod_id'].".html\">{$row['prod_name']}</a></td>";

Am I on the right track? I am just unsure how to pass the last variable (pid) succesfully without showing it!

Thanks,
Simon

Compare Electricals

Hi Simon, It looks from your

Hi Simon,

It looks from your example clean URL that the pid=123 is fixed, so you only need to include it in the RewriteRule "as is" - i.e. not derived from part of the URL using $1, $2 etc.

Have a go with this:

RewriteRule ^(.*)-(.*).html$ productView.php?prod_ean=$1&prod_name=$2&pid=123 [L]

Cheers!
David.

Hi David, Thanks for your

Hi David,

Thanks for your quick response.

Unfortunately the pid variable isn't fixed, it is generated dynamically along with the other 2 variables. I have edited my last post to make it a bit clearer.

Thanks,
Simon

Compare Electricals

Hi, The trouble is, in

Hi,

The trouble is, in order to get "re-written", it has to be in the visible URL somewhere... Am I correct in thinking that in:

www.gynogapod.co.uk/1111111111-qwerty.html

prod_ean=1111111111
prod_name=qwerty

...in which case pid is not present (unless i've misunderstood)....

Hope i'm making sense!

Cheers,
David.

Hi David, Yes thats right, I

Hi David,

Yes thats right, I want to show the url => www.gynogapod.co.uk/1111111111-qwerty.html which doesn't have the pid variable in it, but the pid variable still needs to be sent. It is the pid variable that is used to get the rest of the product info from the database.

God, hope i'm making sense now!!

Thanks,
Simon

Compare Electricals

Hi Simon, That's how I

Hi Simon,

That's how I understood it originally - this rule:

RewriteRule ^(.*)-(.*).html$ productView.php?prod_ean=$1&prod_name=$2&pid=123 [L]

...should do the trick. This would convert:

http://www.gynogapod.co.uk/1111111111-qwerty.html

..into:

http://www.gynogapod.co.uk/productView.php?prod_ean=1111111111&prod_name=qwerty&pid=123

Is that not what you want?

Cheers,
David.

Thanks David, Ok, i got that

Thanks David,

Ok, i got that bit, the part I am struggling with is how the href constructing the url would look like? If i use this

<a href="".$row['prod_ean']."-".urlsafe($row['prod_name'])."&".$row['prod_id'].".html">$row['prod_name']</a>

it shows like this => http://www.gynogapod.co.uk/1111111111-qwerty&123.html

Thanks,
Simon
Compare Electricals

Hi, Ok, I see now - if it

Hi,

Ok, I see now - if it needs to come through in the URL, are you looking for a URL like this:

http://www.gynogapod.co.uk/1111111111-qwerty.html?pid=123

...in which case, you need the PHP:

<a href="".$row['prod_ean']."-".urlsafe($row['prod_name']).".html?pid=".$row['prod_id']."">$row['prod_name']</a>

...and this time, the corresponding RewriteRule:

RewriteRule ^(.*)-(.*).html$ productView.php?prod_ean=$1&prod_name=$2&%{QUERY_STRING}[L]

Cheers!
David.

Thanks David, Is there any

Thanks David,

Is there any way of removing the appended pid on the end to have a "clean" url?

I think the answer to the above is no in which case I will either have to include the pid parameter in the rewritten url or rewrite it another way. Would I be correct in my assumptions?

Thanks,
Simon.

Compare Electricals

Hi Simon, That's correct -

Hi Simon,

That's correct - it's has to be there somewhere i'm afraid.

Cheers,
David.