URL is one of the most important things when it comes to search engine indexing. Compare http://www.yourdomain.com/category/name_of_article_with_keywords to http://www.yourdomain.com/text.php?id=237364. Which one has more chances to be better indexed? Obviously, the one with keywords in URL.
I’m going to explain how to do it in PHP, provide example source code and explain how it works.
There are two easy steps.
1. Redirect all requests to your PHP class
There are two ways of sending all requests to specific PHP file.
a) Simple way. It can be done via managing of 404 error processing (when user requests virtual directory it triggers error 404, which your script should be able to process and render requested page).
Open httpd.conf file, insert the following line (there is a section with other ErrorDocument settings), save the file and restart Apache:
ErrorDocument 404 /index.php
b) Correct way. If your Apache has mod_rewrite installed (in most cases it has), redirection can be setup via this module.
Open httpd.conf file, insert or uncomment the following block (there is a section which starts with “<IfModule mod_rewrite.c>” string), save the file and restart Apache:
<IfModule mod_rewrite.c>
RewriteEngine on
# we don’t want to redirect requests for static images from /graph/ directory
RewriteRule /(graph.*) /$1 [skip=10]
# we also can skip file types by extension
RewriteRule /(.*\.(jpg|gif|swf|pps|ppt|doc|zip|png|pdf)) /$1 [skip=10]
# make webalizer stats accessible the standard way
RewriteRule /(statistics.*) /$1 [skip=10]
# …and redirect all the rest to index.php
RewriteRule /.* /index.php
</IfModule>
Now, when all the requests are being redirected to your index.php file, on the next steps let’s see the source code.
2. Process The Request
I’m assuming the following directory structure:
/index.php
/include.php
/project/engine.php
index.php:
BODY(); ?>
include.php:
engine.php:
DEF = new def(); $this->PARSEURL(); } function PARSEURL() { if ( ($GLOBALS["HTTP_SERVER_VARS"]["CONTENT_LENGTH"]) || ($GLOBALS["_SERVER"]["CONTENT_LENGTH"])) { // В В В processing POST data } else { if ($this->INSTALLATION == “IIS”) { $URI = $GLOBALS["HTTP_SERVER_VARS"]["HTTP_REFERER"]; } else if (isset($GLOBALS["HTTP_SERVER_VARS"]["REQUEST_URI"])) { $URI = $GLOBALS["HTTP_SERVER_VARS"]["REQUEST_URI"]; } else { $URI = $GLOBALS["_SERVER"]["REQUEST_URI"]; } $this->URL = $URI; $URI = ereg_replace(”^/”, “”, $URI); $URI = ereg_replace(”$upath/$”, “”, $URI); if (!$URI) $URI = “/”; if ($URI) { $URIarray = split(”/”,$URI); $num = count($URIarray); $this->URIarray = $URIarray; // setting global navigation variables // $this->SECTION = ($URIarray[0]?strtolower($URIarray[0]):$this->DEF->SECTION); if ( in_array($this->SECTION,$this->DEF->SECTIONS) ) { $this->CATEGORY = ($URIarray[1]?strtolower($URIarray[1]):$this->DEF->CATEGORY); $this->PAGE = ($URIarray[2]?strtolower($URIarray[2]):$this->DEF->PAGE); } else { // В В В section is not known, process it as 404 error $this->SECTION = “404″; } } } } // PARSEURL function BODY() { $body = ‘‘. ‘‘. ‘‘. ‘Search Engine Friendly URL example by ontoinfo.com’. ‘ ‘. ‘‘. ‘‘. ‘‘. ”. ‘URL: ‘. $this->URL . ‘ ‘ . ”. ‘SECTION: ‘. $this->SECTION . ‘ ‘ . ”. ‘CATEGORY: ‘. $this->CATEGORY . ‘ ‘ . ”. ‘PAGE: ‘. $this->PAGE . ‘ ‘ . ‘‘. ‘‘. ‘this example is provided by ‘. ‘ontoinfo.com’. ‘‘. ‘‘. ‘‘; echo $body; } // BODY } ?>
Conclusion
The example above is extremely simple yet effective way to incorporate search engine friendly URL in PHP-based web site. You may copy and paste appropriate source code into your files or you can download the example.
4 comments ↓
That is actually the BAD way to do it.
I’ve used 404 error’s before and it gets you all kinds of weird problems with older browsers etc.
The good way is to use URL rewriting.
Create a .htaccess like this:
#——————-
RewriteEngine On
RewriteBase /your_current_subdir/ #if your site doesnt run in a subdir, use /
RewriteRule ^includes/ - [L] [OR] #don’t apply to direct requests to /includes
RewriteRule ^images/ - [L] # and don’t apply to direct requests to /images
# match up to 5 levels deep and pass that to index.php
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)?$ index.php?$1/$2/$3/$4/$5 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)?$ index.php?$1/$2/$3/$4 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)?$ index.php?$1/$2/$3 [L]
RewriteRule ^([^/]+)/([^/]+)?$ index.php?$1/$2 [L]
RewriteRule ^([^/]+)?$ index.php?$1 [L]
#————-
index.php:
schizoduckie, you are absolutely right.
1.a is not the best way.
In the article 1.b indicates _correct_ way of doing this.
I also provided simplest way (1.a) just because lots of people find it difficult to use RewriteEngine. Besides, on Windows systems, I’d recommend to use httpd.conf instead of .htaccess.
I have visited your site 113-times
I have following problem. Can you please explain why is that happend?
RewriteRule ^jouets-.*-([0-9]+)-.*-([0-9]+)-.*-([0-9]+)\.php$ /jouet-sousrubrique.php?r=$1&sr=$2&ar=$3 [L]
RewriteRule ^jouets-.*-([0-9]+)-.*-([0-9]+)-.*-([0-9]+)\.php$ /jouet-sousrubrique.php?r=$1&sr=$2&arr=$3 [L]
RewriteRule ^jouets-.*-([0-9]+)-.*-([0-9]+)-.*-([0-9]+)\.php$ /jouet-sousrubrique.php?r=$1&sr=$2&pr=$3 [L]
RewriteRule ^jouets-.*-([0-9]+)-.*-([0-9]+)-.*-([0-9]+)\.php$ /jouet-sousrubrique.php?r=$1&sr=$2&prr=$3 [L]
but it always get the first one. if I come from other query strings it always get the first one.
Please explain.
You must log in to post a comment.