I'm working on Ubuntu 14.04.2 Apache 2.4.7
I have a Virtual host set up to serve files from /var/www/dev and the config file looks like this for dev.conf:
<VirtualHost *:80>
ServerAdmin ubuntu#12.234.567.89
DocumentRoot /var/www/dev
ServerName dev.site.com
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/dev/>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
I have a number of files in the /var/www/dev directory that I don't want indexed hence the -Indexes option
However I have a wordpress installation here too and I want those files to be served into a browser properly. The index file is index.php and is located in /var/www/dev/
I also have an .htaccess file in this same document root that looks like this:
DirectoryIndex index.php index.html
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RedirectMatch 301 ^/terms-and-conditions\.php$ /terms-of-use
RedirectMatch 301 ^/privacy\.php$ /privacy-policy
</IfModule>
The DirectoryIndex should tell the browser to use the index.php file, but I am getting a 403 Forbidden error.
I've tried:
Opening up permissions, recursively changing ownership of /var/www/dev to www-data:www-data
Adding DirectoryIndex index.php to the dev.conf file within the <Directory /var/www/dev/> tag - above and below the Options item
Checking mod_dir is active (it is)
Changing the indexes option back to +Indexes - when I load my site it displays the physical file index
refreshing browser when I'm given the 403 or directory index will load my page correctly
Clearing the browser cache every time I test in the browser
Looked in my apache2.conf file and can confirm there is no other reference to DirectoryIndex
Removing all references to DirectoryIndex and changing option to +Indexes - this makes everything work OK except users can see the directory index for all folders including the ones I don't want them to access
Any help is much appreciated!
EDIT:
Using chrome and firefox dev tools - if I click the option for Disable Cache (when dev tools are open) - the site loads fine - perhaps this is a clue
EDIT 2:
error.log outputs this line-
AH01276: Cannot serve directory /var/www/dev/: No matching DirectoryIndex (index.php,index.html) found, and server-generated directory index forbidden by Options directive
access.log just shows my browser details -
"GET / HTTP/1.1" 200 1462 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0"
Related
I have to be able to direct a GET or POST request when it comes in to
/eSCL/ScannerStatus
or
/eSCL/ScannerStatus/
TO
/eSCL/ScannerStatus/index.php
I am NOT looking for a redirect, as that is what I am getting now. (301 Permanently moved)
It is to say whether trailing slash or not, the request references a directory and I need to load index.php in that directory with NO redirect
I have reviewed the docs here
https://httpd.apache.org/docs/2.4/mod/mod_dir.html
and quite frankly it does not seem to work as stated or maybe I am missing a mod?
I have tried many variations .
I have run
sudo a2enmod rewrite
and
sudo a2enmod dir
both now show as enabled.
Apache 2.4 running on Raspberry Pi3
I have tried MANY MANY different rules and I also see in google results that this is a problem for many others trying to do the same. I see numerous "solutions" posted many of which do not work.
I see no reason to ressurect the 20+ mod rewrite attempts that I have made and failed . I am looking for what works without a redirect
Edit-UPDATE-------------------------------------------------
I just got back to this , deleted .htaccess, and now server refuses to load index.php and shows a directory listing instead.
This leaves me perplexed. I do not believe I need .htaccess for PHP files as it was not there before.
EDIT UPDATE 2------------------------------------
I managed to get back on track with some directives that seemed to go away for some reason.
I now have in /var/www/html/.htaccess
DirectorySlash Off
RewriteEngine On
RewriteRule ^eSCL/ScannerStatus/?$ eSCL/ScannerStatus/index.php [L]
RewriteRule ^eSCL/ScannerCapabilities/?$ eSCL/ScannerCapabilities/index.php [L]
RewriteRule ^eSCL/ScanJobs/?$ eSCL/ScanJobs/index.php [L]
RewriteRule ^eSCL/Scans/?$ eSCL/Scans/index.php [L]
in the global secion of apache2.conf I have (this is apparently what I had to do to get index.php files to load as default again):
<Directory "/var/www/html">
DirectoryIndex index.php index.htm
</Directory>
<Directory "/var/www/html/scans/*">
DirectoryIndex index.php index.htm
</Directory>
<Directory "/var/www/html/eSCL/ScannerStatus">
DirectoryIndex index.php index.htm
</Directory>
<Directory "/var/www/html/eSCL/ScannerCapabilities">
DirectoryIndex index.php index.htm
</Directory>
<Directory "/var/www/html/eSCL/ScanJobs">
DirectoryIndex index.php index.htm
</Directory>
Here is what wireshark captures when requesting http://192.168.1.50/eSCL/ScannerStatus (no trailing slash)
HTTP/1.1 301 Moved Permanently
Date: Thu, 06 Feb 2020 16:56:00 GMT
Server: Apache/2.4.25 (Raspbian)
Location: http://192.168.1.50/eSCL/ScannerStatus/
Content-Length: 327
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved here.</p>
<hr>
<address>Apache/2.4.25 (Raspbian) Server at 192.168.1.50 Port 80</address>
I have also specifically excluded the sites-enabled files in apache2.conf with
# Include the virtual host configurations:
# IncludeOptional sites-enabled/*.conf
I have also verified:
pi#raspberrypi:/etc/apache2 $ sudo a2enmod rewrite
Module rewrite already enabled
pi#raspberrypi:/etc/apache2 $ sudo a2enmod dir
Module dir already enabled
/eSCL/ScannerStatus
If ScannerStatus is a physical directory then by default, mod_dir will append the slash with a 301 redirect in order to "fix" the URL.
You need to override this behaviour and set DirectorySlash Off. You will then need to internally rewrite the request using mod_rewrite to the desired index.php document (instead of relying on DirectoryIndex as you would normally).
For example, in your root .htaccess file:
DirectorySlash Off
RewriteEngine On
RewriteRule ^eSCL/ScannerStatus/?$ eSCL/SCannerStatus/index.php [L]
Note that you will need to clear your browser cache as the 301 redirect to append the slash (by mod_dir) will most likely have been cached by the browser.
I do not believe I need .htaccess for PHP files as it was not there before.
You need .htaccess if you want to override default server behaviour and use physical directories without trailing slashes.
UPDATE:
<Directory "/var/www/html">
DirectoryIndex index.php index.htm
</Directory>
<Directory "/var/www/html/scans/*">
DirectoryIndex index.php index.htm
</Directory>
<Directory "/var/www/html/eSCL/ScannerStatus">
DirectoryIndex index.php index.htm
</Directory>
<Directory "/var/www/html/eSCL/ScannerCapabilities">
DirectoryIndex index.php index.htm
</Directory>
<Directory "/var/www/html/eSCL/ScanJobs">
DirectoryIndex index.php index.htm
</Directory>
The <Directory> container applies to the stated directory and all subdirectories. So in the above, all except the first <Directory "/var/www/html"> container are not required.
However, there's nothing here that is enabling .htaccess overrides? And the behaviour you are currently seeing would seem to suggest that .htaccess files are not enabled - since the .htaccess directives don't appear to be doing anything.
TEST: Add any "nonsense" to the top of the .htaccess file. If .htaccess files are being processed then this will break horribly and result in a 500 Internal Server Error. If no error then the .htaccess file is being ignored (ie. .htaccess files are not enabled).
The rewrite we added in .htaccess to route requests to index.php negates the need for DirectoryIndex - but there is no harm adding this.
To enable .htaccess overrides you need to set AllowOverride in the relevant <Directory> container. For example:
<Directory "/var/www/html">
# Allow .htaccess files to override config directives
AllowOverride All
# Allow mod_dir to issue an internal subrequest for the DirectoryIndex
# when requesting /subdirectory/ (note the trailing slash)
DirectoryIndex index.php index.htm
# Access needs to be permitted (somewhere)
Require all granted
</Directory>
If you have access to the server config then you don't need .htaccess. All the directives in .htaccess (a directory context) can go directly in the relevant <Directory> container. (Although development/distribution requirements could make the use of .htaccess files easier.)
Incidentally, the DirectoryIndex directive can be used in .htaccess.
Aside:
As noted earlier, the above rewrite in .htaccess negates the need for DirectoryIndex index.php. However, they can be modified slightly to make use of this. For example (an academic excercise):
DirectoryIndex index.php
DirectorySlash Off
RewriteEngine On
# Note the different regex having removed "/?"
RewriteRule ^eSCL/ScannerStatus$ eSCL/SCannerStatus/index.php [L]
Now, a request for /eSCL/ScannerStatus (no trailing slash) is rewritten using mod_rewrite to index.php. But a request for /eSCL/ScannerStatus/ (with a trailing slash) is now handled by DirectoryIndex which returns index.php via an internal subrequest. With the earlier RewriteRule, both these requests were handled by mod_rewrite.
Note that the 4 rules for the 4 different sub-subdirectories can potentially be combined into a single rule.
ErrorDocument 404 /pageNotFound.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^profile/([0-9a-zA-Z-_]+)/message/?$ message.php?user=$1 [NC,L]
#
#RewriteRule ^profile/([0-9a-zA-Z-_]+)/([0-9a-zA-Z-_]+)/?$ profile.php?user=$1&content=$2 [NC,L]
#
RewriteRule ^profile/([0-9a-zA-Z]+)/?$ profile.php?u=$1 [NC,L]
#
RewriteRule ^story/([0-9a-zA-Z-_]+)/?$ blog_post.php?url=$1 [NC,L]
#
RewriteRule ^forums/questions/([0-9a-zA-Z-_]+)/edit/?$ question_edit.php?url=$1 [NC,L]
#
RewriteRule ^forums/questions/([0-9a-zA-Z-_]+)/?$ question.php?url=$1 [NC,L]
#
RewriteRule ^forums/contribution/([0-9a-zA-Z-_]+)/edit/?$ contribution_edit.php?url=$1 [NC,L]
#
RewriteRule ^forums/contribution/([0-9a-zA-Z-_]+)/?$ contribution.php?url=$1 [NC,L]
#
RewriteRule ^forums/tags/([a-z-]+)/?$ allTags.php?tag=$1 [NC,L]
#
Above is my .htaccess file and I am using WAMP.
At first when I did this, everything works fine, but later on, some URLs started misbehaving for example if I type in localhost/profile/debbie after the page finishes loading the url changes to localhost/profile/Debbie/?u=debbie, though the page contents work fine. I just don't know why. But if I do something like localhost/profile/the_king the url remains the same after loading.
But later on the if I do this localhost/profile/the_king I started getting an error message of undefined index u
Secondly I have a Subdomain under WAMP for my mobile site which uses an IP address of 192.168.106.1 and I use a duplicate of the above .htaccess file for it, but if I do this 192.168.106.1/profile/the_king it just get back my pageNotFound.php error page back.
This is really getting me angry because I watched some video tutorials and I did exactly what they did but I just don't know why mine is misbehaving
Edit here is my configuration for my Subdomain
# Tells Apache to identify which site by name
NameVirtualHost *:80
# Tells Apache to serve the default WAMP Server page to "localhost"
<VirtualHost 127.0.0.1>
ServerName localhost
DocumentRoot "C:/wamp/www/"
</VirtualHost>
# Tells Apache to serve Client 1's pages to "client1.localhost"
# Duplicate and modify this block to add another client
<VirtualHost 127.0.0.1>
# The name to respond to
ServerName localhost
# Folder where the files live
DocumentRoot "C:/wamp/www/mobile/"
# A few helpful settings...
<Directory "C:/wamp/www/mobile/">
Allow from all
Order Allow,Deny
# Enables .htaccess files for this site
AllowOverride All
</Directory>
# Apache will look for these two files, in this order, if no file is specified in the URL
DirectoryIndex index.html index.php
</VirtualHost>
<VirtualHost 192.168.56.1>
# The name to respond to
ServerName localhost
# Folder where the files live
DocumentRoot "C:/wamp/www/"
# A few helpful settings...
<Directory "C:/wamp/www/">
Allow from all
Order Allow,Deny
# Enables .htaccess files for this site
AllowOverride All
</Directory>
# Apache will look for these two files, in this order, if no file is specified in the URL
DirectoryIndex index.html index.php
</VirtualHost>
<VirtualHost 192.168.106.1>
# The name to respond to
ServerName m.localhost
# Folder where the files live
DocumentRoot "C:/wamp/www/mobile/"
# A few helpful settings...
<Directory "C:/wamp/www/mobile/">
Allow from all
Order Allow,Deny
# Enables .htaccess files for this site
AllowOverride All
</Directory>
# Apache will look for these two files, in this order, if no file is specified in the URL
DirectoryIndex index.html index.php
</VirtualHost>
Trying to get a test Laravel project running on the server. If I navigate to the sites URL I am presented with the Laravel welcome screen but none of the routes are working.
To fix this I have changed the .htaccess file as specified by the laravel website.
The problem is in order for the .htaccess to be utilised I have to "AllowOverride all" in my virtualhost file which is causing a 403 to be presented instead of the welcome page.
The error I'm getting is:
Forbidden
You don't have permission to access / on this server.
Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request
Just for testing purposes I have given the entire laravel folder 777 to make sure there isn't a a file permission issue.
Any help would be greatly appreciated. Thanks
Content of my .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Content of my VirtaulHosts:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/testProject1/public
DirectoryIndex index.php
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<Directory> must refer to an absolute path, whereas <Location> can refer to a path relative to the document root.
As such, change:
<Directory />
to:
<Directory /var/www/testProject1/public>
And don't forget to reboot Apache.
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
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 /