Creating SEO friendly URL using htaccess

Creating SEO friendly URL requires a knowledge on how htaccess works. Not just that, you should also be familiar with Regular Expressions but I won’t cover that one in this post though since we’ll be focusing on putting those in htaccess. Now, let’s get our hands dirty.

For example:
http://mysite.com/profile.php?username=JuanDelaCruz

If you wanted that one to become
http://mysite.com/user/JuanDelaCruz/

This is what you’re going to do:

#RewriteEngine On
#RewriteCond %{REQUEST_FILENAME} !-d 
#RewriteCond %{REQUEST_FILENAME}\.php -f 
#RewriteRule ^(.*)$ $1.php

# Use the following rule if you want to make the page like a directory
RewriteCond %{REQUEST_URI} !^/user/profile.php
RewriteRule ^user/(.*[^/])$ user/$1/ [R=301,L]

# The following rewrite the other way round:
RewriteCond %{REQUEST_URI} ^/profile.php
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /profile.php
RewriteCond %{QUERY_STRING} id=([^&]+)
RewriteRule ^profile.php$ user/%1?

There are comments, so hope you can understand it from there.
Hope this helps someone! Have a Happy Coding! :)

How to create a zip file in PHP

TweetHowdy! I will show you how to create a zip file in PHP. All the directory in your server will also be zipped in the code. I will use the ZipArchive class in php. It’s easy as 1 2 3 function ListFiles($dir) { if($dh = opendir($dir)) { $files = Array(); $inner_files = Array(); while($file = readdir($dh))…

Displaying all database records in a dropdown in Yii

TweetI will show you how to display all database records of a certain table in a dropdownlist in Yii. Model Code: class UserType extends CActiveRecord { public function GetUserType(){ $connection=Yii::app()->db; $command= $connection->createCommand("SELECT * FROM ".$this->tableName()); $rows = $command->queryAll(); return CHtml::listData($rows,'id', 'type'); } } View Code: echo $form->dropDownList($model,'user_type_id',UserType::model()->GetUserType()); The CHTML::listData() takes 3 parameters. 1st is the…

Yii Date validation in rules

TweetHere’s how to specify a date validation rules in the Model. The idea here is that “Date To” should always be greater than “Date From”. Here’s how to do that public function rules() { return array( array( 'date_to','compare','compareAttribute' => 'date_from','operator'=>'>', 'allowEmpty'=>'false', 'message' => '{attribute} should be greater than "{compareValue}".'), ); } Piece of cake eh?…

Implementing Paypal Subscription API in PHP

TweetI’m working on implementing Paypal Subscription API in PHP for the past weeks, I thought this was kinda complicated just like the DirectPayment method API. Guess what, it was just a piece of cake, i’ll show you my code: <form name="_xclick" id="_xclick" action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input type="image" src="http://www.paypal.com/en_US/i/btn/btn_subscribe_LG.gif" border="0" name="submit" alt="Make payments with PayPal – it's…

Setup Aweber Email Parser

Tweet5ad3izx5tyarjaj I will show you how to setup AWeber Email Parser. To those of you who do not know what Aweber is, it is an email marketing software that allows you to store email addresses and other important stuffs to people who uses your product or whatever purpose you have. So my idea here is…

About Elson...
Elson Solano is a computer programmer/web developer from The Philippines.
He is a great guy who loves to learn from the experts, so if you wish to share something feel free to contact him.
Spending time with his love-ones and hitting the gym is what he does whenever he's not in front of a square object (monitor).

Stackoverflow.com Profile profile for Elson Solano on Stack Exchange, a network of free, community-driven Q&A sites
Advertisements
Stuff you might be interested
Your Ad Here
Know how Techs