I've recently put up a new site, and had real trouble trying to get search engine friendly URL's working.
The following URL,
http://www.yoursite.com/index.php?view=5
isn't very nice for search engines, as they tend to ignore the variable part, just leaving
http://www.yoursite.com/index.php
to be indexed! This is because they don't want to index what they deem is dynamic content (signified by the variables).
The solution, if you can configure it on your host is to change the URL to
http://www.yoursite.com/index.php/view/5
or, even better
http://www.yoursite.com/view/5
This requires a combination of Apache configuration (using mod_rewrite) and PHP code to understand the new variables.
If your host doesn't have mod_rewrite, then your pretty much out of luck. Either move, or badger them to death to install it.
Apache ConfigurationFirst you need to create a file call .htaccess (note the period in front of the file name).
Here's what I've got in the file.
RewriteEngine on
RewriteOptions MaxRedirects=10
RewriteCond $1 !^(favicon.ico|index\.php|inc|images|css|robots\.txt)
RewriteRule ^(.*)$ index.php?$1 [L]
Break it downRewriteEngine on
This turns on the Apache rewrite engine, to allow the incoming URL (/view/5) to be turned into another URL (index.php?/view/5).
RewriteOptions MaxRedirects=10
Sets the maximum redirects, so that we don't inadvertently end up in a loop.
RewriteCond $1 !^(favicon.ico|index\.php|mailer\.php|inc|images|css|robots\.txt)
The guts. This says that if the URL doesn't contains:
favicon.ico
index.php
mailer.php
inc
images
css
robots.txt
then follow the next rule. You'd add any other files here. For php files in directories, you only need to add the directory name. For example, if you had http://www.yoursite.com/app/index.php, you could just add the app name into the list above. Don't forget to escape the periods in filenames!
RewriteRule ^(.*)$ index.php?$1 [L]
The rule. It takes what ever was entered as the URL and converts it to the new format. The new format is not seen by the user. They continue to see http://www.yoursite.com/view/5.
So now, instead of having http://www.yoursite.com/index.php?view=5, I have http://www.yoursite.com/view/5. The search engine can know successfully spider your site, without knowing that it's dynamically generated!
PHP CodeIn PHP, the script is know being called as index.php?view/5
To get hold of the string, just use
$actionPath = $_SERVER['QUERY_STRING']
The variable $actionPath will know contain the string "view/5".
$actions = split("/", $actionPath)
You will now have an array $actions, which will show
Array {
[0] = "view"
[1] = "5"
}
In your script you can now decide what to process as before!
Hope this help someone out there.
Technorati:
mod_rewrite |
Apache |
PHP |
SEO |
Search Engines