Powered by Blogger.

How to enable mod_rewrite on Apache?

The question is – how do I check if mod_rewrite is enabled? If it is disabled, how to I enable it on Apache on CentOS?

To enable mod_rewrite, you need to make sure that you have it as a module, and to make sure that Apache has loaded the module. And then you need to ensure that Apache is configured to make use of the .htaccess file for each directory.
Follow this article and you should be able to enable mod_rewrite.
First, type the following command on your SSH shell or local terminal:
httpd -V
This will output something like the following:
[root@server ~]# httpd -V
Server version: Apache/2.2.15 (Unix)
Server built: Aug 13 2013 17:27:11
Server loaded: APR 1.3.9, APR-Util 1.3.9
Compiled using: APR 1.3.9, APR-Util 1.3.9
Architecture: 32-bit
Server MPM: Prefork
threaded: no
forked: yes (variable process count)
Server compiled with....
-D APACHE_MPM_DIR="server/mpm/prefork"
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_SYSVSEM_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=128
-D HTTPD_ROOT="/etc/httpd"
-D SUEXEC_BIN="/usr/sbin/suexec"
-D DEFAULT_PIDLOG="run/httpd.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_LOCKFILE="logs/accept.lock"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="conf/mime.types"
-D SERVER_CONFIG_FILE="conf/httpd.conf"
See the lines that are bolded in the above snippet. The HTTPD_ROOT and the SERVER_CONFIG_FILE together give you the location of Apache2′s httpd.conf. For my server it is located at the following directory:
/etc/httpd/conf/httpd.conf
And your server’s module directory is located at the following:
/etc/httpd/modules
Now you have located your httpd.conf and Apache2′s modules directory.
Lets check if mod_rewrite exists as a module:
ls /etc/httpd/modules | grep mod_rewrite
This will either output the following, or nothing at all:
[root@server ~]# ls /etc/httpd/modules | grep mod_rewrite
mod_rewrite.so
If you see mod_rewrite.so, you already have the module and may just need to load it. Chances are remote that you will not see the above. But if you don’t, you may need to install the mod_rewrite module and may need to compile Apache with the module support.
Now, use the following command to check if mod_rewrite is loaded or enabled:
grep -i LoadModule /etc/httpd/conf/httpd.conf | grep rewrite
This will output something like the following:
[root@server ~]# grep -i LoadModule /etc/httpd/conf/httpd.conf | grep rewrite
LoadModule rewrite_module modules/mod_rewrite.so
If you see the above, mod_rewrite has been enabled.
If you see the following, remove the # from the beginning:
#LoadModule rewrite_module modules/mod_rewrite.so
If you don’t see any of these, add the following line to your httpd.conf:
LoadModule rewrite_module modules/mod_rewrite.so
Now you have confirmed that mod_rewrite is available as a module and it is loaded.
Next, to use .htaccess to do URL rewriting using mod_rewrite, you need to allow each directory to override Apache’s global options.
Type in the following command:
grep -i AllowOverride /etc/httpd/conf/httpd.conf
This will output:
[root@server ~]# grep -i AllowOverride /etc/httpd/conf/httpd.conf
AllowOverride None
You need to replace None with All if it is not already All.
Thats it! You have now enabled mod_rewrite and are all set to use URL Rewriting on your Linux CentOS box!
    Blogger Comment
    Facebook Comment