mod_rewrite on Apache(2) Server
Mod_Rewrite uses a rule-based rewriting engine (based on a regular-expression parser) to rewrite requested URLs on the fly. It supports an unlimited number of rules and an unlimited number of attached rule conditions for each rule to provide a really flexible and powerful URL manipulation mechanism. The URL manipulations can depend on various tests, for instance server variables, environment variables, HTTP headers, time stamps and even external database lookups in various formats can be used to achieve a really granular URL matching.
In most Apache servers the mod_rewrite module is not activated by default. If you wish to have your URL’s changed on the fly and rewritten into nice, human readable format then you will need to enable this module. Follow the steps below to enable mod_rewrite.
Open a Terminal window and type:
- sudo a2enmod
You will be presented with the following, but you can safely ignore what is in front of you and just follow the next steps.
- Which module would you like to enable?
- Your choices are: actions alias asis auth_basic auth_digest authn_alias authn_anon authn_dbd authn_dbm authn_default authn_file authnz_ldap authz_dbm authz_default authz_groupfile authz_host authz_owner authz_user autoindex cache cern_meta cgid cgi charset_lite dav_fs dav dav_lock dbd deflate dir disk_cache dump_io env expires ext_filter file_cache filter headers ident imagemap include info ldap log_forensic mem_cache mime mime_magic mod_python negotiation php5 proxy_ajp proxy_balancer proxy_connect proxy_ftp proxy_http proxy rewrite setenvif speling ssl status suexec unique_id userdir usertrack version vhost_alias
- Module name?
Enter the module name “rewrite” and press enter. Now you need to open the default file with sudo rights and change the “AllowOverride” option from “None” to “All” in the first 2
- sudo pico /etc/apache2/sites-enabled/000-default
Below is what it should look like when you are finished.
<VirtualHost*>- ServerAdmin root@xpheas.com
- DocumentRoot /var/www
<Directory/>- Options FollowSymLinks
- AllowOverride All
</Directory><Directory /var/www>- Options Indexes FollowSymLinks MultiViews
- AllowOverride All
- Order allow,deny
- allow from all
</Directory>
Restart the webserver.
- sudo
/etc/init.d/apache2 force-reload
Now the module is enabled, all that is left is to rewrite your .htaccess for URL changing.
Now that you have mod_rewrite enabled you are going to want to use it. In my next tutorial I will show you how to rewrite your .htaccess file to change ugly URLS into nice URLS. An exmaple of each is below so you can understand what I mean.
Ugly URL
- http://example.com/blog/?p=17
Nice URL, which of course is more search engine friendly and will provide much better SEO.
- http://example.com/blog/index.php/2007/09/17/dont-tase-me-bro/

by Soddengecko @ 6:45

No one has opened their mouth about
“mod_rewrite on Apache(2) Server”