Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable mod_rewrite for Apache 2.2

I've got fresh install of Apache 2.2 on my Vista machine, everything works fine, except mod rewrite.

I've uncommented

LoadModule rewrite_module modules/mod_rewrite.s 

but none of my rewrite rules works, even simple ones like

RewriteRule not_found %{DOCUMENT_ROOT}/index.php?page=404 

All the rules I'm using are working on my hosting, so they should be ok, so my question is, is there any hidden thing in apache configuration, that could block mod rewrite?

like image 773
Jakub Arnold Avatar asked May 15 '09 14:05

Jakub Arnold


People also ask

How do I enable a2enmod?

In order for Apache to understand rewrite rules, we first need to activate mod_rewrite . It's already installed, but it's disabled on a default Apache installation. Use the a2enmod command to enable the module: sudo a2enmod rewrite.

What is Apache mod_rewrite?

mod_rewrite is an Apache module that allows for server-side manipulation of requested URLs. mod_rewrite is an Apache module that allows for server-side manipulation of requested URLs. Incoming URLs are checked against a series of rules. The rules contain a regular expression to detect a particular pattern.


2 Answers

In order to use mod_rewrite you can type the following command in the terminal:

sudo a2enmod rewrite 

Restart apache2 after

sudo /etc/init.d/apache2 restart 

or

sudo service apache2 restart 

or as per new unified System Control Way

sudo systemctl restart apache2 

Then, if you'd like, you can use the following .htaccess file.

<IfModule mod_rewrite.c>     RewriteEngine On     RewriteBase /     RewriteCond %{REQUEST_FILENAME} !-f     RewriteCond %{REQUEST_FILENAME} !-d     RewriteRule . /index.php [L] </IfModule> 

The above .htaccess file (if placed in your DocumentRoot) will redirect all traffic to an index.php file in the DocumentRoot unless the file exists.

So, let's say you have the following directory structure and httpdocs is the DocumentRoot

httpdocs/     .htaccess     index.php     images/         hello.png     js/         jquery.js     css/         style.css includes/     app/         app.php 

Any file that exists in httpdocs will be served to the requester using the .htaccess shown above, however, everything else will be redirected to httpdocs/index.php. Your application files in includes/app will not be accessible.

like image 103
Jonathan Mayhak Avatar answered Oct 21 '22 17:10

Jonathan Mayhak


For my situation, I had

RewriteEngine On 

in my .htaccess, along with the module being loaded, and it was not working.

The solution to my problem was to edit my vhost entry to inlcude

AllowOverride all 

in the <Directory> section for the site in question.

like image 29
Mike Wazowski Avatar answered Oct 21 '22 16:10

Mike Wazowski