Using Mod Rewrite


Using Mod Rewrite

 

Mod Rewrite is a very useful apache module. It allows you to rewrite and modify url's.

This article will show you how to confirm it is installed on your server and how to use it.

 


For servers with the Plesk control panel:

 

Log into your server via ssh as root.

 

Mod Rewrite is enabled by default on plesk servers. You can verify this with the following command:

 

apachectl -M | grep rewrite

 

modify the vhost.conf file at /var/www/vhosts/example.com/conf/vhost.conf

 

replace "AllowOverride None" with "AllowOverride all".

 

restart apache with the following command: service httpd restart

 

create a .htaccess file in the httpdocs folder under the domain. (see example below)

 

 


For servers with the Cpanel control panel:

 

Log into your server via ssh as root.

 

Mod Rewrite is enabled by default on cpanel servers. You can verify this with the following command.

 

httpd -l | grep rewrite

 

create .htaccess file in the public_html folder under the domain. (see example below)

 

 


For CentOS servers without a control panel:

 

Log into your server via ssh as root.

 

Mod Rewrite is usually enabled by default with apache on CentOS installs.

You can verify this with the following command.

 

apachectl -M | grep rewrite

 

Open the Apache configuration file located at /etc/httpd/conf/httpd.conf (or alternate vhost configuration file if you have a custom setup)

 

Change AllowOverride None to AllowOverride All

 

restart apache with the following command: service httpd restart

 

create .htaccess file in the public_html folder under the domain. (see example below)
 


For Ubuntu servers without a control panel:

 

The default installation of Apache2 comes with mod_rewrite installed.

 

To enable mod rewrite issue the following command:

 

a2enmod rewrite

 

Modify the file /etc/apache2/sites-available/default (or your custom vhost configuration file if different)

and replace every occurrence of "AllowOverride None" with "AllowOverride all".

 

Restart apache with the following command: service apache2 restart
 

create .htaccess file in the public_html folder under the domain. (see example below)

 


Testing Mod Rewrite:

 

Create a .htaccess containing the following and upload it to the root of your site:

 

RewriteEngine On

RewriteRule ^google.html$ http://www.google.com/ [R=301]

 

 

Going to http://www.yoursite.com/google.html should redirect you to Google.com if you have mod rewrite enabled and your syntax is correct.

 

 

For more information on Mod rewrite see the following links.

 

http://httpd.apache.org/docs/current/mod/mod_rewrite.html

http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

 

This article has been provided by Codero Hosting, the leading provider of reliable Dedicated, Cloud, Hybrid, and Managed hosting services. Need more information on this topic or to learn more about Codero’s hosting services please visit www.codero.comchat with us online or give us a call at 866-2-CODERO.

Posted in
Last update:
2015-12-03 20:39
Average rating:0 (0 Votes)