5

I have been trying to get my .htaccess file to work on my localhost. I know that the file works because it is on my server and works. However it doesn't seem to be working locally.

I have followed the tutorials I could find to set overrides to all, below is my /etc/apache2/sites-available/default file start:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    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>

This is the code in the same directory for my nibble site:

<VirtualHost *:80>
    ServerName nibble.local
    DocumentRoot /var/www/nibble_framework/web/
    <Directory /var/www/nibble_framework/web/>
        AllowOverride All
        Options Indexes FollowSymLinks MultiViews
        Allow from All
    </Directory>
    RewriteLog /var/www/rewrite.log
    ErrorLog /var/log/apache2/error.log
</VirtualHost>

I have enabled this site using a2ensite and restarted apache. I have also added the site to my hosts file:

  127.0.0.1  nibble.local

I have enabled mod rewrite using a2enmod and restarted/reloaded apache multiple times.

  $ a2enmod rewrite 
  Module rewrite already enabled

My .htaccess file is in /var/www/nibble_framework/web/.htaccess and has the following code:

  Options +FollowSymLinks +ExecCGI

  <IfModule mod_rewrite.c>
  RewriteEngine On

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^([^/]*)/?.*$ - [E=FILE:%{DOCUMENT_ROOT}/$1.php]

  RewriteCond %{ENV:FILE} !^$
  RewriteCond %{ENV:FILE} -f
  RewriteRule ^([^/]*)/?(.*)$ $1.php?url=$2 [QSA,L]

  RewriteCond %{ENV:FILE} !^$
  RewriteCond %{ENV:FILE} !-f
  RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
  </IfModule>

When I do a print_r($_REQUEST), it is allways an empty array even when the url is poplulated by a long string.

Does anyone have any idea why this might be failing?

Edit:

apache2 access log:

~$ tail -f /var/log/apache2/access.log
::1 - - [16/Aug/2011:07:32:39 +0100] "OPTIONS * HTTP/1.0" 200 152 "-" "Apache/2.2.16 (Ubuntu) (internal dummy connection)"
::1 - - [16/Aug/2011:07:32:39 +0100] "OPTIONS * HTTP/1.0" 200 152 "-" "Apache/2.2.16 (Ubuntu) (internal dummy connection)"
::1 - - [16/Aug/2011:07:32:39 +0100] "OPTIONS * HTTP/1.0" 200 152 "-" "Apache/2.2.16 (Ubuntu) (internal dummy connection)"
::1 - - [16/Aug/2011:07:32:40 +0100] "OPTIONS * HTTP/1.0" 200 152 "-" "Apache/2.2.16 (Ubuntu) (internal dummy connection)"
::1 - - [16/Aug/2011:07:32:40 +0100] "OPTIONS * HTTP/1.0" 200 152 "-" "Apache/2.2.16 (Ubuntu) (internal dummy connection)"
::1 - - [16/Aug/2011:07:32:40 +0100] "OPTIONS * HTTP/1.0" 200 152 "-" "Apache/2.2.16 (Ubuntu) (internal dummy connection)"
::1 - - [16/Aug/2011:07:32:40 +0100] "OPTIONS * HTTP/1.0" 200 152 "-" "Apache/2.2.16 (Ubuntu) (internal dummy connection)"
::1 - - [16/Aug/2011:07:32:40 +0100] "OPTIONS * HTTP/1.0" 200 152 "-" "Apache/2.2.16 (Ubuntu) (internal dummy connection)"
::1 - - [16/Aug/2011:07:32:40 +0100] "OPTIONS * HTTP/1.0" 200 152 "-" "Apache/2.2.16 (Ubuntu) (internal dummy connection)"
::1 - - [16/Aug/2011:07:32:40 +0100] "OPTIONS * HTTP/1.0" 200 152 "-" "Apache/2.2.16 (Ubuntu) (internal dummy connection)"
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Luke
  • 153
  • 1
  • 1
  • 6
  • have you enabled the apache rewrite log and seen what it's generating as output? http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritelog – EightBitTony Aug 16 '11 at 08:42
  • I added RewriteLog /var/www/rewrite.log to the nibble site virtual host file, the rewrite.log file is empty though, I have made its permissions 777 just in case but all urls I try produce no entries into it. – Luke Aug 16 '11 at 09:56
  • is the access log for your virtualhost seeing any traffic? – EightBitTony Aug 16 '11 at 10:01
  • I have pasted from /var/log/apache2/access.log above – Luke Aug 16 '11 at 10:03
  • `ls -l .htaccess`? – Warren Young Aug 16 '11 at 11:26
  • Hi Warren, what do you mean with your above command? – Luke Aug 16 '11 at 12:47
  • He means what are the permissions on the .htaccess file, i.e. can the web server even read it. When you go to http://nibble.local, what response do you get and does it show up in the access log? – EightBitTony Aug 16 '11 at 17:55
  • First off, move the access and error logs from your nibble.local site to a different file -- make sure that apache is actually acknowledging your virtualhost directive. – Shadur Aug 17 '11 at 05:00
  • @Shadur, I added the RewriteLog command to the nibble virtual host file and it created the log file, that log file was not there before so it does work. EightBitTony, the site works, navigation works around the site when view files in the web root, but the URL parameter is not being passed by the .htaccess file. Warren $ ls -l /var/www/nibble_framework/web/.htaccess -rwxrwxrwx 1 luke luke 428 2011-08-14 07:49 /var/www/nibble_framework/web/.htaccess I changed it to 777 and still no joy – Luke Aug 17 '11 at 08:16
  • With 777 it actually parses urls when using index.php, however when running another .php file such as admin.php, it fails to parse the URL. So a successful url would be nibble.local/testPage and url parameter = testPage. A fail is nibble.local/admin/testPage where url is not parsed as request parameter. admin forwards to admin.php but testPage is ignored. So it is mostly working. Should I transfer to stackoverflow.com? – Luke Aug 17 '11 at 08:26
  • 1
    777 is a TERRIBLE PERMISSION SET. Change that *right now* to 644 for files and 755 for directories and stuff that should be executable. – Shadur Aug 17 '11 at 10:21
  • @shadur, I am aware its not secure to have 777 permissions but its only local and I wanted to make sure it wasn't a permissions issue. – Luke Aug 17 '11 at 10:34
  • Do not remove localhost from the 127.0.0.1 line. Lots of things will fail if you do. I would not rule out that it has something to do with the prob at hand. – Alien Life Form Jan 17 '12 at 14:32
  • @Luke: Have to tried my posted answer? If so, comment under it. – J. M. Becker Jan 20 '12 at 00:42

2 Answers2

3

I suspect your VirtualHost is not getting processed correctly, which would then not set AllowOverride for the get requests. First your /etc/hosts file should look like this..

127.0.0.1   localhost   localhost.localdomain
127.0.1.1   nibble.local

Second, You must have that interface set for namebased vhosts. You did not specify if you have it currently set. Usually its set in the main apache config, I'm guessing ubuntu/debian has that file located /etc/apache2/apache2.conf. Make sure this line is set somewhere in that file,

NameVirtualHost *:80

Once you make these changes, restart apache

sudo /etc/init.d/apache2 restart

Noe test it out, remember you only set that hosts entry locally. So as is this will only work correctly when requested from localhost. If you get stuck, or it doesn't work, post the relevant lines from /var/log/apache2/error.log

J. M. Becker
  • 4,831
  • 3
  • 28
  • 40
1

Specify AccessFileName directive to use .htaccess file.

AccessFileName .htaccess

Enable the rewrite engine and then restart the service.

sudo a2enmod rewrite 
Nikhil Mulley
  • 8,145
  • 32
  • 49