Recommended:

  • phpclasses.org
  • jsclasses.org
  • jsmag.com
  • siteapps.com
  • View our reviews on Hot Scripts
  • JS Tutorial
  • scripts.com
  • securesignup.com




Recent Comments

Powered by Disqus




Back to articles

Using MOD rewrite to make site search friendly

Here is the way to rewrite your site URLs to more search friendly way. For example address http://example.com/index.php?category=5&subcategory=1 doesn't say anything to search engines nor to site visitors. Much better way is rewriting URL as a path like http://example.com/category-name/subcategory-name. It tells search engines and visitors that now they are viewing subcategory of category and of course they expect to see whole category by accessing link http://example.com/category-name.

Another thing to keep in mind is that you should rewrite all inside HTML URLs to absolute paths. It means if you had path like "./styles/style.css" no it should look like (if you are in root directory) "/styles/style.css" or "http://example.com/styles/style.css".

Wait there's more. Now you won't be selecting category by it's ID, you will be selecting category by it's name, so first thing is to make sure category name is unique. And of course you can't allow all symbols in URL, so it is better to create another field in your database especially for page link and make sure that it is unique for every category.

Here is a function which can give you an example how to make page link name out of category name:

<?php
function create_pagename()
{
    //array of characters you don't want to allow
    $bad_char = array("!", "@", "#", "$", "%", "^", "&", "*", 
                      "{", "}", "|", "'", "?", "/", "<", ">", 
                      "-", "_", "=", "+", "(", ")", "[", "]", 
                      ";", ":", ",", ".", '\', "'", '"', "|");
    //taking out all bad characters
    $page_name = str_replace($bad_char, "", $category_name);
    
    //replacing spaces by another seperator like "-"
    return $page_name = str_replace(" ", "-", $page_name);
}

$page = create_pagename("Gr[ea]t: category $$$!");
?>

Then to rewrite your URLs simply create .htaccess file in your root directory with this content:

#some settings
Options +FollowSymLinks
Options +Indexes

#enabling mode re_write
RewriteEngine On

#if file or directory exists do not rewrite
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

#or specify file types which you don't want to rewrite
#RewriteCond %{REQUEST_URI} !.(css|jpg|gif|png|js)$ [NC]

#remember rewrite condition applies only to the next rule
#so if you have many rules, you should write these condition 
#before every rule that you don't want to rewrite your files

#rewrite rule
RewriteRule ^([^/]*)/([^/]*)$ /index.php?category=$1&subcategory=$2 [L]

After that you need to use links like href="/categoryname/subcategoryname" in your HTML so in PHP they will be interpreted as ?category=categoryname&subcategory=subcategoryname, thus giving you $_GET['category'] and $_GET['subcategory'] variables.


You may also be interested in:

Powered by BlogAlike.com

blog comments powered by Disqus