URL Rewrite Using htaccess File

In this article, we will see how to change URL using .htaccess file. Good URL helps to improve page ranking in search engine results. It is possible to change URL of webpage using .htaccess file. Rewritten URL have better structure and looks good in browser address bar. Here, we are using different URL structure to rewrite using htaccess. URL rewrite is a very strict and powerful rule to change existing URL that are easy for search engine. In this tutorial I  will show you how to do htaccess rewrite url to another url. Have a look.

URL Rewriting Using .htaccess File

You may like :

Protect Directory With Password Using htaccess

How to Set Up a Custom 404 Error Page Using htaccess

 

Discuss with different URL Structure for rewriting.

RewriteEngine On
RewriteBase /

RewriteEngine is component of software which is used here to perform rewrite operation on Uniform Resource Locators (known as URL) to change its appearance.
In above snippet RewriteBase is used here to define base for rewrites. If your project folder or directory is in root the not need to define RewriteBase. Whenever we use subdirectory for project or working on localhost for rewrite, then we need to define directory for Rewritebase. See below snippet to understand it clearly.

RewriteEngine On
RewriteBase /url-rewrite/

Here, url-rewrite is subfolder which we are using project development. Let us see some different URL Structure and their rewriting using .htaccess.

Structure 1:

Old URL 
http://demos.phpcluster.com/demo/url-rewrite/result.php

Rewritten URL
http://demos.phpcluster.com/demo/url-rewrite/result.html

htaccess snippet to rewrite.
RewriteRule ^results\.html$       result.php

Structure 2:

Old URL 
http://demos.phpcluster.com/demo/url-rewrite/result.php?id=5

Rewritten URL
http://demos.phpcluster.com/demo/url-rewrite/result/

htaccess snippet to rewrite.
RewriteRule ^([a-zA-Z0-9_-]+)$      result.php?id=$1

Structure 3:

Old URL 
http://demos.phpcluster.com/demo/url-rewrite/r result.php?id=5&val=me

Rewritten URL
http://demos.phpcluster.com/demo/url-rewrite/result/5


htaccess snippet to rewrite.
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$      result.php?id=$2&val=$3

Structure 4:

Old URL 
http://demos.phpcluster.com/url-rewrite/result.php?id=3

Rewritten URL
http://demos.phpcluster.com/url-rewrite/vikash/result.html

htaccess snippet to rewrite.
RewriteRule ^vikash/result\.html$     result.php?id=$4

Structure 5:

Old URL 
http://demos.phpcluster.com/url-rewrite/result.php?id=7

Rewritten URL
http://demos.phpcluster.com/url-rewrite/result-9.html

htaccess snippet to rewrite.
RewriteRule ^result-([0-9]+)\.html$    result.php?id=$7

Structure 6:

Old URL 
http://demos.phpcluster.com/url-rewrite/result.php?id=8

Rewritten URL
http://demos.phpcluster.com/url-rewrite/result/test.html

htaccess snippet to rewrite.
RewriteRule result/(.*)\.html$     result.php?id=$9

Note:
If you are using this above snippet to rewrite and having some issue, You can implement /?.html$ instead of \.html$ . That’s it. If you have any query regarding URL rewrite please comment.

Leave a Comment