Tuesday, June 22, 2010

How to do custom redirection of a website

Usually the redirection needs when a website changed/move to different location though it may use for lots of other purposes. In this post, I have explain how you can do it by using .htaccess

Say, your site has lots of URL and you want to redirect some of those which query string has a parameter name “option” and the value is “redirect”.

Below are the codes to redirect into new URL:
RewriteEngine on
RewriteCond %{QUERY_STRING} option=redirect
RewriteRule (.*) http://newsite.com/index.php?%{QUERY_STRING} [R=301,L]

Just copy above lines into .htaccess file and place it to root directory in your application.

Some helpful posts:

Sunday, June 20, 2010

Change file extension in URL

In my previous article I have shown how you can use different extension of a file instead of .php explicitly but in this post I have explain how to rewrite the URL instead of renaming the file extension explicitly.

To rewrite the URL you must add following lines in a .htaccess file and place it to root directory of your application.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.htm$ $1.php [nc]

The above code will rewrite the product.php to product.html.

And following is the code to pass parameters with URL
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.html(.*)$ $1.php$2

Some related posts:

Hide the fact that the web pages are written in PHP

Sometimes you may want to hide the fact of your web site’s written language to visitors then you can do it easily by using .htaccess Say, you have developed your site by using PHP and wanted to show it as ASP then you can do it by using following tricks:

Just add following line in the .htaccess file and place it to application root directory.
AddType application/x-httpd-php .asp
Now everyone have to use all files of the application by using .asp though the application was written in PHP.