0

I am running an Apache server on my Raspberry Pi with PHP, and I want to create a .htaccess file to redirect 404 errors and remove .html extensions on URLs. I have tried simply running 'sudo nano .htaccess' and a new file was created, but after I updated it with 404 error redirection (and saved the file) it didn't work. Could anyone help me with making a working .htaccess file?

EDIT The output of ls -la /var/www is:

total 216
drwxr-xr-x 14 alex root  4096 Feb 16 07:58 .
drwxr-xr-x 12 root root  4096 Jan 17 21:26 ..
-rw-------  1 alex alex  6375 Feb 15 21:16 .bash_history
drwxr-xr-x  2 alex alex  4096 Jan 24 08:55 blog
drwxr-xr-x  9 alex alex  4096 Jan 25 20:18 .cache
drwxr-xr-x  8 alex alex  4096 Jan 25 20:21 .config
drwxr-xr-x  3 alex alex  4096 Jan 24 14:55 d
drwx------  3 alex alex  4096 Jan 25 20:15 .dbus
drwxr-xr-x  3 alex alex  4096 Feb 16 07:58 Desktop
drwx------  2 alex alex  4096 Jan 25 20:15 .gvfs
-rw-r--r--  1 root root    30 Jan 24 14:55 .htaccess
drwxr-xr-x  2 alex alex  4096 Feb 15 20:40 .idlerc
-rw-r--r--  1 alex alex  1770 Jan 24 09:47 index.php
drwx------  3 alex alex  4096 Jan 25 20:17 .local
drwxr-xr-x  3 alex alex  4096 Jan 25 20:15 .scratch
drwxr-xr-x  2 alex alex  4096 Jan 22 19:37 style
drwx------  4 alex alex  4096 Feb 15 20:33 .thumbnails
-rw-r--r--  1 alex alex 99678 Jan 21 17:11 wagicallogo.ico
-rw-r--r--  1 alex alex 15719 Jan 21 17:10 wagicallogo.png
-rw-------  1 alex alex   100 Feb  8 13:05 .Xauthority
-rw-------  1 alex alex 17621 Feb 15 21:16 .xsession-errors

And my 404 error page is /d/404.html

EDIT 2 Here is my 'ls -la /var/www/d':

total 24
drwxr-xr-x  3 alex alex 4096 Jan 24 14:55 .
drwxr-xr-x 14 alex root 4096 Feb 16 07:58 ..
-rw-r--r--  1 alex alex  925 Jan 24 14:51 404.html
-rw-r--r--  1 root root 1084 Jan 24 11:30 group.php
  • Can we see the contents of the .htaccess file and the output of ls -la for the directory where the .htaccess file is located? – Steve Robillard Feb 16 '15 at 8:05
  • Why is /var/www pointing to alex's home directory? – Bex Feb 18 '15 at 9:12
2

You have a couple of problems. The first was the ownership of the files they need to be owned by the user that Apache runs as (root in this case).To correct that do the following:

chown root:root .htaccess

and

chown root:root 404.html

The second problem is with the default Apache configuration. To correct this edit /etc/apache2/sites-enabled/000-default

and change:

AllowOverride None

to

AllowOverride All

Then restart Apache:

sudo service apache2 restart

You should also not be hosting a site from your home directory. If for no other reason than it will make maintenance a hassle and is a security issue (you are making those files accessible via the web). The default location for web files is /var/www/ you should place your web files there instead.

  • When I do 'sudo nano /etc/apache2/sites-enables/000-default' it gives me a blank file. – Gale Feb 16 '15 at 10:30
  • what is in that directory and how did you install apache – Steve Robillard Feb 16 '15 at 10:31
  • Ah, I fixed it now. It wasn't /sites-enables, it was /sites-enabled. Don't know why, but it should be fixed now. Thanks! – Gale Feb 16 '15 at 10:32
  • Is it fixed? If so I am goinmg to close this question as off topic – Steve Robillard Feb 16 '15 at 11:06
2

I had this problem and nothing worked, all the .conf files where perfect. Then I thought about activating mod_rewrite on apache with

sudo a2enmod rewrite

followed by

systemctl restart apache2

Bingo, it started working..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.