Why does directory not forward to index.html but instead to 403 error - .htaccess

I have a website with a main directory and a child directory. When I enter www.example.com it properly goes to the index.html (www.example.com/index.html) but when I do www.example.com/childdirectory it forwards me to 403 instead of www.example.com/childdirectory/index.html. Why does it do this and how do I fix this?
I already tried putting this code in my .htaccess but it didn't work:
DirectoryIndex index.html

Related

Options +Indexes of .htaccess not working

I have a website running on a server. It's just an index.html with a hello world text. Also, I have a folder named /vpn which contains various txt files and an index.html file.
If I try to access the URL domain/vpn, it shows me the content of index.html.
I just need to show the files inside the folder vpn when the user tries to access domain/vpn.
I created an .htaccess file with the next content in the root:
RewriteEngine on
<If "%{REQUEST_URI} == '/vpn/'">
DirectoryIndex disabled
Options +Indexes
</If>
When I try to access to vpn, it shows me a 404 error, the requested URL was not found on this server.
.htaccess is applying the DirectoryIndex rule (If a delete it, it shows me index.html content again), but not the Options +Indexes one.
I tried the same example in localhost (with XAMPP) and it's working fine.
What can be the problem?
PD: This is the content of apache2.conf file:
When I try to acces to vpn, it shows me a 404 error, the requested URL was not found on this server.
If you are getting a "404 Not Found" then it would imply that mod_autoindex is not actually installed on your server (consequently Options +Indexes has no effect - although it would seem from your server config that Indexes is perhaps already enabled).
mod_autoindex is the module responsible for generating the directory listings.
I created an .htaccess file with the next content in the root:
Personally, I would create an additional .htaccess file in the /vpn directory instead:
DirectoryIndex disabled
Options +Indexes
And disable Indexes (and set DirectoryIndex) in the root .htaccess file.
NB: RewriteEngine has no place here, unless you are overriding a parent config.
If I try to access the url "domain/vpn"
Note that you should be requesting domain/vpn/ (with a trailing slash). If you omit the trailing slash then mod_dir issues a 301 redirect to append it.

How to deny all access to a directory and redirect 403?

I have a folder on my webserver and want to deny all access to that folder and all the files in it. Additionally, the visitor must be redirected to a specific error page on the same server.
The secured folder path is:
https://www.mywebsite.com/some_folder/files
The error page is located here:
https://www.mywebsite.com/some_folder/error.php
I tried placing a .htaccess file in the 'files' folder. This does prevent visitors from accessing the folder and files, but the redirect does not take place. I suspect that the syntax of the relative path is not correct.
This is my .htaccess:
ErrorDocument 403 /../error.php
Order deny,allow
Deny from all
When hitting the secured folder URL, the browser gives me this error message: "A 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request."
Does someone have an example of what my .htacces should be?
You may try this in some_folder/.htaccess:
RewriteEngine On
RewriteEngine !^error\.php$ error.php [L,NC]
This will rewrite all requests that start with /some_folder/ to some_folder/error.php though browser won't see status code 403.
Inside error.php you may use this php code to return 403:
http_response_code(403);

Redirect everything to 404 except files in public folder

How redirect everything inside rootfolder to 404, except files in root/public folder? Directory listing must be disabled everywhere and .htaccessredirected to 404 as well. It's possible to make that with one .htaccess file?
Currently i have Redirect 404 / in root, but when trying to access .htaccess, it's forbidden. Thanks
To redirect everything inside rootfolder to 404, except files in root/public folder ,you can use the following Rule in your /root/.htaccess .
RewriteEngine on
RewriteRule !public - [R=404,L]
and to disable directory listing for all folder/subfolders, add this line to your /root/.htaccess
Options -Indexes

a custom 404 page for all my subdomains.

I want all my subdomains to have the same custom 404 error page. In the htaccess file of the htdocs folder I have tried the following solutions:
ErrorDocument 404 /www/errordocument/404.php
ErrorDocument 404 www/errordocument/404.php
ErrorDocument 404 http://www.mydomain.com/errordocument/404.php
None of them seems to work except for the last one. Only this one gives a 302 redirect and changes the url in the address bar. Which I don't want. I can make a 404 file + htaccess file in all the subdomains only than if someone goes to a non existing subdomain they dont get the custom 404.
how can this be done?
note: server has Apache 2.0 handler
You can keep /errordocument/ folder directly under DocumentRoot of parent domain and then have this directive in the .htaccess of parent domain:
ErrorDocument 404 /errordocument/404.php
After this under all the sub domain's DocumentRoot folder (which will be a sub directory under parent's DocumentRoot) create symbolic links like this:
cd sub1; ln -s ../errordocument; cd -
cd sub2; ln -s ../errordocument; cd -

using htaccess how can I specify index.html as the home page with both index.html and index.php on the server?

As my title says, using htaccess how can I specify index.html as the home page with both index.html and index.php on the server ? I have a regular basic html site on the server and I want to start working on a joomla site which of course uses php. I don't want to redirect the php if I enter in the index.php into the address to allow me to start working on the joomla installation. I just want to automatically go to index.html when the domain name is entered. I hope I'm making sense. Thanks
You use DirectoryIndex to specify, which file is shown, when just the directory is requested
DirectoryIndex index.html

Resources