In this discussion, we will learn more about Apache module mod_rewrite that is used rewrite request URLs on the fly with a rule-based rewrite engine. We hope this discussion can be the best guidelines activate module mod_rewrite in Apache web server. In the Apache documentation site, mod_rewrite is truly the Swiss army knife of URL manipulation. It’s extremely powerful tool based on regular expression. With this feature, web administrator may use extensively for the purpose of controlling client access attempts to our websites.

If we use Linux and when installing Apache as web server, we will need to deicide whether we will modules statically or build the as shared objects. In this discussion, we assume to use shared object. The loading of shared object modules is handled by mod_so.

mod_rewrite-process

Apache module mod_rewrite can be activated by editing Apache configuration file in the global environment section. For more detail, see sample httpd.conf file of Apache configuration on Microsoft Windows OS bellow :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
	### Section 1: Global Environment
ServerRoot "%CDPMS:SRVDIR%Apache"
ScoreBoardFile "%CDPMS:RTDIR%apache_runtime_status"
PidFile "%CDPMS:RTDIR%httpd.pid"
 
……………………
 
#LoadModule mime_magic_module modules/mod_mime_magic.so
#LoadModule proxy_module modules/mod_proxy.so
#LoadModule proxy_connect_module modules/mod_proxy_connect.so
#LoadModule proxy_http_module modules/mod_proxy_http.so
#LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule negotiation_module modules/mod_negotiation.so
 
LoadModule rewrite_module modules/mod_rewrite.so
 
LoadModule setenvif_module modules/mod_setenvif.so
 
	…………

Search keyword “mod_rewrite” or add in the global environment section as sample httpd.conf above, and uncomment it. After edit and save httpd.conf file, then restart Apache webserver services. How to do if we use Linux or *nix operating system ? Apache module mod_rewrite can be activated by editing httpd.conf file in the Apache installation directory, such as “/etc/apache2/httpd.conf”. Next step after editing configuration file and restart Apache

        #/etc/init.d/httpd restart

If we use Linux based on Debian distribution, we can do very easy and simple with run command bellow :

	#a2enmod mod_rewrite

After run command as above, restart Apache web server , as shown in the code bellow :

	#/etc/init.d/apache2 restart

After running procedure above, we have activated Apache module mod_rewrite and the next step, test our configuration to redirect URL.

Before test our configuration, create .htaccess file in the root web directory and type as code bellow :

1
2
3
4
5
6
7
8
 
 
	RewriteEngine On
	RewriteBase /
 
	RewriteCond %{REQUEST_FILENAME} !-f
    	RewriteCond %{REQUEST_FILENAME} !-d
    	RewriteRule ^(.*)$ index.php/$1 [L]

With sample code above, we will redirect all URL to our website to index.php. Hopefully, we now have mod_rewrite installed and we are ready to start using it. Get more explanation how to create .htaccess to redirect custom URL in the next discussion.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • Slashdot
  • Technorati
  • YahooMyWeb