einstein
einstein

Reputation: 13850

How to activate mod_rewrite?

I know that this question have been asked several times. But I can't get it to work.

I installed Apache2 in my Ubuntu server I can also confirm that mod_rewrite is installed using phpinfo();. I have also put a file called .htaccess in my root folder(/var/www/.htaccess). In my .htaccess file I paste the following code:

Options +FollowSymLinks 
RewriteEngine On 
RewriteRule ^.*$ test.php

So everything is redirected to test.php

But it still doesn't work. So I checked my httpd.conf file under /etc/apache2. It is completely empty, with no lines of code (This seems a little odd to me)?! However checking in Stackoverflow answers there should be at least the following code in httpd.conf:

<Directory />
    Options FollowSymLinks
    AllowOverride All
    Order deny,allow
    Deny from all
    Satisfy all
</Directory>

So I paste that code in httpd.conf. And restarted Apache with sudo /etc/init.d/apache restart. And it still does not work?

I have also tested to open the file /sites-enabled/000-default and /sites-available/default, where all virtual host settings lies and change under the directory /var/www and / to AllowOverride All. The mod_rewrite still doesn't work. Can anyone please help me. This problem has been baking my nuts for a while.

Also, my apache2.conf file doesn't work. I tried to write som nonsense. And it is still gives me the normal result instead of http 500 error

Upvotes: 10

Views: 29806

Answers (6)

Sliq
Sliq

Reputation: 16484

Because this thread ranks very high on google, here another solution:

WHAT'S CAUSING THE PROBLEM ?

It's caused because the standard definition in apache simply blocks all .htaccess stuff. Seriouslsy. This seems to be the initial state in Ubuntu 12.04 LTS.

SOLUTION

In /etc/apache2/sites-available/default (that's a file, not a folder) there's a code block like

<Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride XXXXXXXXXXXXXXXXXXX
    Order allow,deny
    allow from all
</Directory>

Here you should change

AllowOverride None

to

AllowOverride All

IMPORTANT

You have to do these changes with sudo rights, anotherwise Ubuntu will not save the file. After a RESTART your .htaccess stuff should work. To test .htaccess simply put some nonsense text into it and surf to localhost or 127.0.0.1 -> If you see "internal server error": voila! Remove the nonsense and you are ready to go.

Upvotes: 5

ThinkingMonkey
ThinkingMonkey

Reputation: 12727

In your question, you have mentioned,

Also, my apache2.conf file doesn't work. I tried to write som nonsense. And it is still gives me the normal result instead of http 500 error

So, before posting more stuff to try-out with .conf files,

run strace -o somefilename.txt sudo /etc/init.d/apache restart

strace - trace system calls and signals : strace(1) - Linux man page.


The -o somefilename.txt part puts the output of strace into somefilename.txt. Open the text file and search for httpd.conf. You should be able to see code similar to:

stat("/etc/httpd/conf/httpd.conf", {st_mode=S_IFREG|0644, st_size=35894, ...})=0
open("/etc/httpd/conf/httpd.conf", O_RDONLY|O_CLOEXEC) = 3

The path to httpd.conf will be replaced by the path to your default httpd.conf

If you find one, then open that httpd.conf and make changes there. Else, a httpd.conf is not getting loaded.

If later is the case, try:

sudo /etc/init.d/apache -f  /path/to/httpd.conf start

Also, check for NameVirtualHost *:80 and make sure it is not commented.

Upvotes: 0

Olivier Pons
Olivier Pons

Reputation: 15778

All suggestions from Niels Bom are the smartest ones (I think). But I would add this as the first suggestion: try to launch and stop apache via the Ubuntu command: then when it's supposed to be stopped, make sure it's stopped ie verify your local page doesn't show anymore.

  • read what's in the folder /etc/apache2/. There should be apache2.conf conf.d envvars httpd.conf mods-available mods-enabled ports.conf sites-available sites-enabled
  • then if everything is ok, take a look at mods-enabled where you will find if the mods are enabled if so, then you can go on, otherwise take a look at the other answers about enabling modrewrite
  • take a look at sites-enabled: it's where you can find the sites that are... (guess what?) enabled. There should be the default website.
  • if so, open this file, and try to put apache nonsense in it, restart the server and see if it's happy or not. If it's not happy that's good. If it's happy then you're working on the wrong file
  • last: try to check what are the authorisations for htaccess files for this default vhost. Here's the way they work: if Apache doesn't find any directive in the vhost file, it will apply the global configuration. So I'd suggest to add the configuration in your vhost file, between the <virtualhost> - </virtualhost> tags (of course).

Please tell me what's going on and I'd be glad to update my question accordingly.

Upvotes: 8

Moshe Shaham
Moshe Shaham

Reputation: 15974

Please check /etc/apache2/apache2.conf

Upvotes: 0

Niels Bom
Niels Bom

Reputation: 9377

Okay, let's check assumptions:

  1. Put nonsense in your Apache config files (and restart Apache to let them take effect), does Apache "complain" on restart? Then it tries to load those files. Try this for every Apache config file you can find. You now have a complete list of Apache config files that are loaded.

  2. put nonsense in your htaccess, reload the page, do you get a 500 error? If not, the htaccess is not getting loaded.

  3. Put the most basic mod_rewrite statement in your htaccess:

    Options +FollowSymLinks RewriteEngine On RewriteRule ^.*$ test.html

This should rewrite every request to test.html.

Try this and give us the results.

Upvotes: 2

bingjie2680
bingjie2680

Reputation: 7773

if you run this command,

sudo a2enmod rewrite

ubuntu will output whethere it is activated or already running.

Upvotes: 10

Related Questions