Thursday, August 15, 2013

Redirect URL with out changing the url in address bar using htaccess

Hi. This is a small trick to redirect the URL to another url with out changing the address bar url. We should write the below code in htaccess file.
RewriteEngine on

RewriteRule (.*)\.com$ $1.php [PT]
RewriteRule (.*)\.in$ $1.php [PT]


This will show an URL to user in the address bar,  but internally it will redirect and execute the code of other URL which we configure here.
After adding this to .htaccess file. check this. 

For example if you want to show url as

http://www.domainname.com/pages/mypage.com

to user and internally we want to call and execute the code of another page like

http://www.domainname.com/pages/mypage.php

then use this sample code in your htaccess file. while executing this, the URL will remain same as 
http://www.domainname.com/pages/mypage.com in address bar, but redirect and executes the content of mypage.php. 
For applying this transfers only to a folder follow this. Then we should write the below code in htaccess file.



RewriteEngine on

RewriteRule ^pages/(.*)\.com /pages/$1.php [PT]
RewriteRule ^pages/(.*)\.in /pages/$1.php [PT]


You can change the pages to your folder. The above code works only when the user requests a page from pages folder. other wise it will ignore this.

mypage.com is redirected to the page of mypage.php and will execute the content of the mypage.php when user type this url in address bar
http://www.domainname.com/pages/mypage.comor 
http://www.domainname.com/pages/mypage.in


Thank you

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Is there any way to do the same with domain also, means, redirect to another domain without changing url?

    ReplyDelete