How to get .htaccess files working with Apache VirtualHost - .htaccess

My problem is that my .htaccess file on my local server is not being read. The settings in the VirtualHost file seem to always take precedence.
I have tried the following:
Enabled mod_rewrite
Changed the AllowOverride to All but this causes a HTTP Error 500 Internal server error. I have tried it with various options but it always causes a 500 error.
I am using a VirtualHost file on Ubuntu which looks like the following:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /web/website
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /web/website>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
</VirtualHost>
In my .htaccess file under /web/website I have the following rules (which are not being read):
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{HTTP_USER_AGENT} ^facebookexternalhit
RewriteRule ^(.*)$ ogtags.php?$1 [L,QSA]
ErrorDocument 404 /404
ErrorDocument 401 /401
One thing I tried which did work was appending these rules directly into the VirtualHost file, but I would like my .htaccess file to work! Is that such a big ask? :(
Edit: So I looked in my apache error.log and it says Invalid command 'Action', perhaps misspelled or defined by a module not included in the server configuration referring to my .htaccess file. There doesn't seem to be a module called Action which I can enable. Any ideas?
Edit 2: I noticed that my httpd.conf file is blank. Should this matter since I am using VirtualHost files?

After looking at my apache error.log I realised I just had to enable the Apache actions module:
sudo a2enmod actions
And no more 500 Internal Server Errors!
Hope this helps somebody down the line :)

You miss the RewriteBase / directive
Add it after the RewriteEngine directive :
RewriteEngine On
RewriteBase /

Related

Codeigniter 4 Multisite

I have installed Codeigniter 4 on Raspbian and everything seems to be working fine.
My web directory is /var/www/html
Inside there are two folders containing two different Codeigniter-4 apps that I would like to invoke with:
blue.ddns.net -> /var/www/html/blue/public/index.php
black.ddns.net -> /var/www/html/black/public/index.php
So I'm creating a .htaccess file to put in the folder /var/www/html/ to handle the two requests
Well, I'm still at step 0 because I can't get the .htaccess file to work properly.
Below I attach a copy of the file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /var/www/html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ blue/public/index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
The Rewrite module is activated
the error that appears in /var/log/apache2/error.log is:
/var/www/html/.htaccess: Expected </IfModule> before end of configuration, referer: https://blue.ddns.net/
That shouldn't really be done in your htaccess. You should setup that in your apache virtual hosts.
Go into your sites-avaiable folder.
$ cd /etc/apache2/sites-available
Create a new virtual host called blue.ddns.net.conf
$ touch blue.ddns.net.conf
Open that file with nano or any other text editor you might like and add the following.
<VirtualHost *:80>
ServerAdmin your#email.com
DocumentRoot /var/www/html/blue/public
ServerName blue.ddns.net
ServerAlias blue.ddns.net
<Directory "/var/www/html/blue/public">
allow from all
AllowOverride All
Options None
Require all granted
</Directory>
RewriteEngine on
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteCond %{SERVER_NAME} =blue.ddns.net
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
Save and close the file.
Then add this to your apache with the following:
$ sudo micro blue.ddns.net.conf
Repeat the same process for the other codeigniter install and reboot apache.
$ sudo service apache2 restart
That should do it.
Now if you want to override some config that would be something that you might do in your htaccess file for each codeigniter 4 install.
I've always worked in hosting and never had to do Apache configurations.
But now I'm discovering the Raspberry/Linux world.
Thanks to your answers I was able to better understand the problem and solve it (for the moment) in the following way:
Now my configuration files 000-default.conf (port 80) and 000-default-le-ssl.conf (port 443) both contain the following code:
<Directory "/var/www/html">.
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
in /var/www/html instead I created a .htaccess file with the following code:
# Disable directory browsing
Options All -Indexes
<IfModule mod_rewrite.c>.
RewriteEngine On
RewriteCond "%{HTTP_HOST}" "black\.ddns\.net"
RewriteRule ^(.*)$ black/public/index.php?/$1 [L]
RewriteCond "%{HTTP_HOST}" "blue\.ddns\.net"
RewriteRule ^(.*)$ blue/public/index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
apps are located in
/var/www/html/blue
and
/var/www/html/black
It's a very simple solution that serves my purposes.
Although I think it's not very elegant
What do you think about it?

laravel virtualhost 404 error

I have apache2 on my ubuntu server.
There is my /etc/apache2/sites-available/laravel.conf
<VirtualHost *:80>
ServerName http://laravel.mysite.com/
DocumentRoot "/var/www/laravel/public"
<Directory "/var/www/laravel/public">
AllowOverride all
Order allow,deny
 Allow from all
</Directory>
</VirtualHost>
and I have open the apache's mod_rewrite function.
There is my /var/www/laravel/app/routes.php
<?php
Route::get('/', function()
{
return View::make('home/index');
});
Route::get('/hehe', function()
{
return "hehe';
});
There is my /var/www/laravel/public/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
and now, I can visit the http://laravel.mysite.com, but when I visit the http://laravel.mysite.com/hehe, it gives the 404 error. Is there anything I config wrong?
Although the question is very old, for anyone still interested (as was me, but managed to solve the problem), I created a checklist to go through if Laravel routes throw a 404 or 403 error.
I assume that Laravel's originally installed public/.htaccess file is unmodified.
Be sure to insert the <Directory> section into the virtual host's apache config file with the proper path.
Be sure to have AllowOverride All, and not None in the <Directory> section to allow routes (avoiding 404 error).
Be sure to have Require all granted, and not denied in the <Directory> section to give permission visitors to see the site (avoiding 403 error).
It may also be necessary to include Options FollowSymLinks in the <Directory> section if you use symlinks in your app.
Be sure to have the rewrite module enabled in apache: $ sudo a2enmod rewrite
And don't forget to restart the apache server for the changes to take effect.
The only thing i do is to devise the apache2.conf, add this:
<Directory /var/www/laravel/public>
Options FollowSymLinks
AllowOverride All
</Directory>

Laravel 4 routing not working due to .htaccess file?

I'm writing my steps and findings here so you can see what I've tried and what results I got. Any advice would be welcomed. I followed the comments in this answer.
I'm running Laravel 4, using XAMMP version 1.8.2 with PHP 5.4.19 and Apache 2.4.4 on a Windows 7 machine and I'm simply still trying to get a local instance up and running.
In my case: http://localhost/sos/sos_public/ is my main screen and that works, but when I try to get to http://localhost/sos/sos_public/signup I get this error: Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException which has been talked about a lot on the net, and then I found GaryJ stating that it might be an .htaccess issue, so I did what he suggested.
My /app/routes.php file looks like this (I've tried this with a / in front of signup too):
Route::get('signup', function()
{
return 'hello!';
});
Route::get('/', function()
{
return View::make('hello');
});
First:
Just for a laugh, see if /index.php/hello works. If so, then it's a .htaccess problem.
http://localhost/sos/sos_public/index.php/signup worked perfectly fine. So it's an .htaccess problem.
My .htaccess file looked like this:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
And:
if you're running Apache 2.4, note the changes since previous
versions, regarding Require all granted and AllowOverride all within a
<Directory />...</Directory> block on your virtual host.
I added this in my httpd.conf file as suggested by Dalton Gore - got the Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException error (changed this directory path to /SOS/sos_public and C:/xammp/htdocs/SOS/sos_public/ and C:/xammp/htdocs/SOS/sos_public - same results):
<Directory />
AllowOverride none
Require all denied
</Directory>
<Directory /SOS/sos_public>
AllowOverride all
Require all granted
</Directory>
Next:
Check if anything in .htaccess is working
I added dsjkdsfghk to my .htaccess file and immediately got an error, so I know my .htaccess file is being used.
Then:
Try removing the IfModule conditional. As you've got access to the
host / vhost, you can soon enable that module if it's not - so it
doesn't need be checked on every request. Equally, try moving it out
of .htaccess, and into a <Directory />...</Directory> block in your
vhost - if you've got nothing else in your .htaccess it can then be
deleted as well.
Having an empty .htaccess file caused a 404 error in my browser for http://localhost/sos/sos_public/signup (http://localhost/sos/sos_public/ still worked).
Removing the .htaccess file from C:\xampp\htdocs\SOS\sos_public\ had the same results.
Then:
Try: <VirtualHost *:80> DocumentRoot
"/Users/amiterandole/Sites/laravelbackbone/public" ServerName
laravelbackbone.dev <Directory /> AllowOverride all Require all
granted </Directory> <IfModule mod_rewrite.c> Options -MultiViews
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^
index.php [L] </IfModule> </VirtualHost>
I did that and just like Amit,
I emptied out my htaccess file and tried the above and now nothing
seems to work.
Except where his laravelbackbone.dev pointed to his Sites folder root, I just got an Error 400 - Bad request on both http://localhost/sos/sos_public/ and http://localhost/sos/sos_public/signup when I ran them in my browser.
My httpd-vhosts.conf file looked like this:
<VirtualHost *:80>
DocumentRoot "C:/xammp/htdocs/SOS/sos_public"
ServerName sos.dev
<Directory />
AllowOverride all
Require all granted
</Directory>
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
</VirtualHost>
and in my .hosts file I obviously had:
127.0.0.1 sos.dev
Lastly:
You've definitely got the conf/extra/httpd-vhosts.conf file be
included (uncommented) within the main httpd.conf?
I have this uncommented - yes.
Another link I tried but to no avail: https://stackoverflow.com/a/17778222/956975 and http://www.epigroove.com/blog/laravel-routes-not-working-make-sure-htaccess-is-working
What should I change where?
What am I missing?
Your ServerName is
sos.dev
And apache take this in consideration, you you must access your routes using:
http://sos.dev/
And NOT
http://localhost/
It might be the error of not enabling rewrite Engine in apache2
In linux,
sudo a2enmod rewrite
sudo systemctl restart apache2
In windows,
You should find httpd.conf file if it's not available in your apache look for apache2.conf file is another name for httpd.conf and enable the rewrite module for more info or here see how to do above-said procedure in this link
Lost httpd.conf file located apache

How to get MAMP to read .htaccess files

I am trying to get the .htaccess working in MAMP. The content of the .htaccess is a simple redirect line, but the entire .htaccess file seems to have no effect, even when I change it to contain invalid data.
Is there any settings within MAMP I need to change to enable .htaccess files?
In httpd.conf on /Applications/MAMP/conf/apache, find:
<Directory />
Options Indexes FollowSymLinks
AllowOverride None
</Directory>
Replace None with All.
Restart MAMP servers.
Go to httpd.conf on /Applications/MAMP/conf/apache and see if the LoadModule rewrite_module modules/mod_rewrite.so line is un-commented (without the # at the beginning)
and change these
from ...
<VirtualHost *:80>
ServerName ...
DocumentRoot /....
</VirtualHost>
To this:
<VirtualHost *:80>
ServerAdmin ...
ServerName ...
DocumentRoot ...
<Directory ...>
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory ...>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
I'm using MAMP (downloaded today) and had this problem also. The issue is with this version of the MAMP stack's default httpd.conf directive around line 370. Look at httpd.conf down at around line 370 and you will find:
<Directory "/Applications/MAMP/bin/mamp">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
You need to change: AllowOverride None
To: AllowOverride All
If you have MAMP PRO you can set up a host like mysite.local, then add some options from the 'Advanced' panel in the main window. Just switch on the options 'Indexes' and 'MultiViews'. 'Includes' and 'FollowSymLinks' should already be checked.
The problem I was having with the rewrite is that some .htaccess files for Codeigniter, etc come with
RewriteBase /
Which doesn't seem to work in MAMP...at least for me.
I have MAMP v 6.6.2 (year 2022), and I was trying to make working php friendly URLs on my localhost Apache, by adding '.htaccess' file to my website root directory ("localhost/mywebsite/.htaccess"):
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?id=$1 [L,QSA]
Which didn't work. But I tested it on cPanel hosting on a real domain and it worked fine.
So I have tried to change httpd.conf with these advises:
https://stackoverflow.com/a/21411933/3936149 - didn't help
https://stackoverflow.com/a/7670598/3936149 - didn't help
https://stackoverflow.com/a/19204983/3936149 - didn't help
In the end I came up to an advise:
https://stackoverflow.com/a/16106140/3936149 - and it worked!
I simply commented out the second line of the code in my '.htaccess' (located at "localhost/mywebsite/.htaccess") file and that's it:
RewriteEngine On
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?id=$1 [L,QSA]
After it started to work I reverted all the changes in httpd.conf file, I have done above, and it was still working.

.htaccess: RewriteEngine not allowed here

I uploaded the .htaccess to the server and received an Error 500 (Internal Server Error).
And in the error log I had the following error:
.../.htaccess: RewriteEngine not allowed here
But mod_rewrite.so is enabled.
So, do I need to change
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
to
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
in the /etc/httpd/conf/httpd.conf file?
Or could it be something else? The .htaccess file should be okay, because it works perfectly fine on my localhost. I just don't want to screw anything up.
Here's part of my .htaccess file:
Options All -Indexes
Options +FollowSymLinks
RewriteEngine On
minimum configuration for your .htaccess to work:
AllowOverride FileInfo Options
allowing all configuration will work as well:
AllowOverride All
Let's say your DOCUMENT_ROOT is /home/foo/web then have this config in your httpd.conf file:
<Directory "/home/foo/web">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
This should take care of RewriteEngine is not allowed error you're getting.
Just an idea, this happened to me, and I've lost a lot of time trying to solve it.
If you have a directory like d:/web/my-sites/some-site/
And you place the .htaccess on d:/web/my-sites/some-site/.htaccess (where it supposed to be).
If you have ANY .htaccess files before this directory, Apache reads that files, and blocks the execution of the entire path, producing an internal server error.
I.E.: You have d:/web/my-sites/.htaccess
In httpd version 2.4 (2.4.3 and 2.4.4), take a look at /etc/httpd/conf.d/wordpress.conf
There is one entry for:
....
Change:
"AllowOverride Options"
to
"AllowOverride All"
in wordpress.conf also in addition to changing httpd.conf. Restart the http server before the changes will take effect.
Also, just make sure you are editing the correct config file. I had created a file under /etc/apache2/users/USERNAME.conf but was editing /etc/apache2/httpd.conf.
Removing the USERNAME.conf file worked.
Try to change username.conf file on Mac under /etc/apache2/users/username.conf to
<Directory "/Users/akyoo/Sites/">
AllowOverride All
Options Indexes MultiViews FollowSymLinks
Require all granted
</Directory>
My .htaccess file looks like this
#Allow from all
Options +FollowSymLinks
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
This works fine for me.
Solution for Ubuntu users
I was getting this type of error from the Google Cloud instance after checking the logs from the /var/log/apache2/error.log
.htaccess: RewriteEngine not allowed here
To get rid of the above error & 500 Internal Server Error, follow these simple steps
sudo nano /etc/apache2/apache2.conf
Then add these snippets of lines
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
After you’ve made that change, make sure to restart the server:
sudo service apache2 restart
This worked for me in getting rid of 500 Internal Server Error hosted on Google Cloud instance
you could use something like this in your .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|html|test)
RewriteRule ^(.*)$ /index.php/$1 [L]
this code simply means, that anything not pointing to index.php or html or test should be directed to index.php!
Hope this is helpful!

Resources