RElated Topics

Friday, November 14, 2008

Virtual host and url redirect or url rewrite

Virtual host and url redirect or url rewrite
Download sample Code as zip
<?php
/** index.php
* Demo for URL Redirect

* Use: You can create vertual url say with Username
http://www.web.com/username/mail
http://www.web.com/username/profile?view=full
where username can be anything and decide on runtime

* .htaccess file needed

* Example: http://web.com/rewuested/vertual/path?g1=v1&g2=v2

* You get all info in _GET as
$_GET = array ( '_req_path_' => '/rewuested/vertual/path',
'g1' => 'v1',
'g2' => 'v2', )

*
* Note: To run .htaccess you need to modify apache httpd.con file ,
* on linux it is generaly at /usr/local/apache/httpd.conf
*/


echo "<br>GET <pre>"; var_dump($_GET); echo "<Pre>";

echo "<br>POST <pre>"; var_dump($_POST); echo "<Pre>";

//
//So finaly you redirect any url to your index.php with path name in _$GET['_req_path_']
//All Other $_GET, $_POST param are as remain untouch
//You can redirect to any other file text.php by chanfing index.php to test.php in .htaccess file


?><form method="POST"><input name="uname"><input type="submit"></form>



.htaccess


<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?_req_path_=$1 [L,QSA]
</IfModule>





httpd.conf



###################
#Settings REquired for url Redirect
###################

#Folowing Line is allowed .htacces name for any access file
AccessFileName .htaccess


<Directory />
#... Other code lies ...
#To allow .htaccess on directory level

AllowOverride All
#... Other code lies ...
</Directory>


###################
#Vertual HOST

###################
#
# Use name-based virtual hosting.
# Without it vitual host want work
#
NameVirtualHost 127.0.0.1


#Use full to run myweb.com from your local apache

#DON'T forgot to set etc/hosts entry like below, To modify it u need admin / root access for OS
# 127.0.0.1 myweb.com www.myweb.com
#Path for hosts win: c:\windows\system32\driver\etc\hosts
#Path for hosts in linux, /etc/hosts
<VirtualHost 127.0.0.1:80>
ServerName myweb.com

ServerAlias www.myweb.com
DocumentRoot /usr/local/apache/htdocs/myweb
DirectoryIndex index.html index.php
ErrorLog logs/myweb_access_log

CustomLog logs/myweb_error_log common
</VirtualHost>