Forum Moderators: phranque
RewriteRule id=(.*)$ info.php?id=$1
but if I want to insert '.$a1['PropertyType'].'_'.$a1[city].'_ '.$a1['country'].' before the id
so for example with the rewrite on the top line my url would look like this:
www.example.com/id=123
But for SEO reasons I would like the url to be:
www.example.com/villa_alicante_spain/id=123
so it would need to insert data from MYSQL fields as shown above by inserting fields like "PropertyType".
Can this be done in htaccess or in the php file itself?
Any help would be great!
Remove the "=" sign as well, since it is a restricted character, and cannot be used in the URL-path-part (it can be used in query strings, but the HTTP rules for URLs are different). You might as well dump the "id" part too, since it serves no SEO purpose in the URL. So, the friendly URL format is now "/villa-spain/123".
Having done that, you then need to add mod_rewrite code to "connect" that new URL to your script filepath. This internal rewrite will involve removing the villa-spain string found as the first 'directory level' in the URL-path, adding "id=" to the number found in the second 'directory level', changing the URL-path to '/info.php' and appending the id=123 query string to it.
Now you need two more steps: You need an external 301 redirect from the old unfriendly '/info.php?id=123'-style URLs/filepaths to the new /villa-spain/123-style URLs, if and only if that old-style URL is directly-requested by a client, and is not occurring due to the action on the previously-described internal rewrite.
This redirect will 'recapture' old bookmark traffic, and also tell search engines to use the new-style URLs, crediting the PageRank/Link-popularity from the old URLs to the new. Because doing this redirect involves knowing the proper villa-spain text string to use in the redirect's friendly URL, your script will have to do this, or you will have to define a RewriteMap at the server config level to call a PERL script to do the database lookup (this cannot be done in .htaccess).
Because the client-requested URL may not be available to PHP (I'm simply not sure), you may have to pass it as an additional query string rewritten to the script. The variable you need is called THE_REQUEST within mod_rewrite, and contains the HTTP method, the URL-path, and the HTTP protocol level, like this:
GET /info.php?id=123 HTTP/1.1-or-
GET /villa-spain/123 HTTP/1.1
You will also need to add code to your script to check incoming friendly URLs against your database: If the villa-spain string does not exactly match the string used for that id number, then generate an external 301 redirect to the correct URL; Otherwise, if you don't check for this there is the potential for creation of infinite duplicate content, because I, as your hypothetical arch-enemy competitor, could link to your page as "/shoddy-overpriced-roach-infested-villa-spain/123, and your server would accept that and serve the id=123 page, because that string really isn't used to define the content for the page with id=123. When this link appeared high in the search results, you'd be rather upset, I would think...
Again, you can use the client-requested URL as described in the previous step to do this villa-spain string checking.
For background information, see this thread [webmasterworld.com] in our Apache Forum library. Because your project involves database lookups, it's a bit more complex, but that thread explains the basic concepts fairly thoroughly.
Jim