0

Currenty, I am using .htaccess file to redirect users to 404.php page when they mis-spell the url.

ErrorDocument 404 /mywebsite/404.php

In my parent directory, there are some folders like css, js in which stylesheets and js files are kept. URL for these directories is like this:

localhost/mywebsite/css
localhost/mywebsite/js

By typing above urls, all the files are listed in the browser and can be opened by clicking on them.

Is there any way to add these urls in .htaccess file and redirect the user to home page (localhost/mywebsite) when they try to type above urls on the browser?

2 Answers 2

2

Options -Indexes add to .htaccess

Sign up to request clarification or add additional context in comments.

3 Comments

I also have a folder say "ABC" which contains PHP files. Now if I type the url "localhost/mywebsite/ABC", all the PHP files are listed. So, as per you, there is no way to restrict the user to type above url?
Okay, it shows forbidden message to user. It is fine for me. But it would be great if I am able to redirect the user to home page instead of showing forbidden message.
Create /mywebsite/403.php add in 403.php <?php header('Location: example.com/'); ?>. Add in .htaccess ErrorDocument 403 /mywebsite/403.php
1

It sounds like you actually want to disable indexing? Have a look at how-do-i-disable-directory-browsing.

2 Comments

@ddtpoison777- Thanks for the link. It shows forbidden message to user. It is fine for me. But it would be great if I am able to redirect the user to home page instead of showing forbidden message
Have a look at stackoverflow.com/questions/4170098/redirect-on-deny-htaccess. PS: the .htaccess, its capabilities and how php works with it is very well documented. I suggest googling as well. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.