I would appreciate very much anyone who can help with the following problems I am having with my .htaccess mod_rewrite rules:
Our site is run using html, php and mysql on shared server using:
Linux operating system
MySQL 5.1.30
Apache version 2.2.15
php version 5.2.13
We are running a real estate site where all properties are referenced from the database as follows (using 555 as property id example):
http:
//www.example.com/accommodation.php?id=555
I am trying to write friendly URL so the 'property type' then id number shows instead.
For example: http:
//www.example.com/Apartment-555
Or http:
//www.example.com/Villa-555
We have property types:
Apartment
Villa
Bungalow
Finca
Townhouse
House
Studio
Cottage
Duplex
I have achieved this but with problems, here is the .htaccess file:
SetEnv TZ Europe/London
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http:
//www.example.com/$1 [L,R=301]
THE ABOVE IS TO MAKE ALL REQUESTS GO TO WWW
RewriteRule ^Apartment-([0-9]+)$ http:
//www.example.com/accommodation.php?id=$1 [P]
RewriteRule ^Villa-([0-9]+)$ http:
//www.example.com/accommodation.php?id=$1 [P]
RewriteRule ^Bungalow-([0-9]+)$ http:
//www.example.com/accommodation.php?id=$1 [P]
RewriteRule ^Finca-([0-9]+)$ http:
//www.example.com/accommodation.php?id=$1 [P]
RewriteRule ^House-([0-9]+)$ http:
//www.example.com/accommodation.php?id=$1 [P]
RewriteRule ^Studio-([0-9]+)$ http:
//www.example.com/accommodation.php?id=$1 [P]
RewriteRule ^Cottage-([0-9]+)$ http:
//www.example.com/accommodation.php?id=$1 [P]
RewriteRule ^Townhouse-([0-9]+)$ http:
//www.example.com/accommodation.php?id=$1 [P]
RewriteRule ^Duplex-([0-9]+)$ http:
//www.example.com/accommodation.php?id=$1 [P]
THE ABOVE ARE WORKING BUT SEE PROBLEMS BELOW
ErrorDocument 404 /404.shtml
THE ABOVE IS A CUSTOM 404 PAGE
All relevant links in all pages have been changed to accomodate the change for example the old links which used to say:
<a href='accommodation.php?id=".$row["id"]."'> now read...
<a href='".$row["accommodation_type"]."-".$row["id"]."'>
And the links work great - showing the friendly URL in the address bar of the browser as: http:
//www.example.com/Apartment-555
PROBLEM;
The issue I have is this... If I change the word 'Apartment' to Bungalow or any other property type in the address bar so it reads:
http:
//www.example.com/Bungalow-555 (it still shows Apartment-555), there isn't a Bungalow-555 in the database so I was sort of expecting a error page!
And I certainly don't want Google to think there is an Apartment-555 and Bungalow-555 and Villa-555 etc etc.
I only want whatever is stored in the database to show as a friendly URL
On another note, I was looking around at other property sites to see if they did the same and came across a site called <snip>.
Ideally we would like our URL's to behave the same as this site and I have spent the past 7 days reading dozen of sites about Apache, Mod_rewrite and friendly URL's, htaccess and more. I have tried dozen of different conotations but still can't seem to get it right.
Here is what happens with the competitor-site site;
If you take a look at anyone one of the properties they have, for example:
http:
//www.competitor-site.co.uk/rentals/mijas-costa/22122
If you enter the above URL into your browser and remove any character or several characters from either the word rentals or the word mijas-costa and hit the enter/return key the misspelled URL jumps back to the original URL above automatically.
Example:
http:
//www.competitor-site.co.uk/reals/mijas-costa/22122 (couple of letter taken out of the word rentals)
http:
//www.competitor-site.co.uk/rentals/mijsta/22122 (couple letters taken out of mijas-costa)
but after hitting the enter key on keyboard
Both incorrect urls jump back to the original;
http:
//www.competitor-site.co.uk/rentals/mijas-costa/22122
I presume this is done with mod rewrite and .htaccess?
I would be so grateful if someone could help me on these issues and how to achieve the desired results?
[edited by: jdMorgan at 5:12 pm (utc) on Apr 18, 2010]
[edit reason] example.com, obscured URLs [/edit]