How to Generate Friendly URLs using htaccess

How to Generate Friendly URLs using htaccess

URLs are easier to use and remember by users and are also better for search engine optimization. Further disclose much better links within a website and adds a higher level of professionalism to our websites.

Anyway, when we have created a dynamic page in PHP you will usually have no friendly URL. How do we become friendly automatically? One way to do this is with a file .htaccess and thanks to Apache and mod_rewrite .

mod_rewrite is an Apache module that allows you to manipulate URLs on the server requested. That is, when a URL request arrives at the server, this is checked against a set of rules created with Regular Expressions created to detect a pattern. If that pattern is in the URL and the condition imposed by the Regular Expression pattern is replaced by a text string or a particular action is met.

In the example that will illustrate we’ll have a list of users to link to their profile using friendly urls.

As we have friendly urls:

http://www.yourdomain.com/users.php?name=juan

And as friendly url would understand:

http://www.yourdomain.com/juan

To achieve this, we need to create a .htaccess file and add the following code:

RewriteEngine On
RewriteCond% {} ^ www.yourdomain.com HTTP_HOST
RewriteRule (. *) Http://yourdomain.com/$1 [R = 301, L]

RewriteEngine On
RewriteRule ^ ([a-zA-Z0-9 _-] +) $ users.php? name = $ 1
RewriteRule ^ ([a-zA-Z0-9 _-] +) / $ users.php? name = $ 1

And modify links to the user profiles by adding the url friendly rather than unfriendly.

Lucid Kit

Related Posts
Leave a reply
Captcha Click on image to update the captcha .