Forum Moderators: phranque

Message Too Old, No Replies

Help needed in URL redirecting to avoid access to subdirectories.

Avoid access to subdirectories on Apache

         

bhushan

6:17 am on Apr 16, 2009 (gmt 0)

10+ Year Member



In my project I want to restrict access to subdirectories on the server and want to give access to only the root location.

So lets say user hits [localhost...] he will be shown the index page but if he types [localhost...] he should not be taken to this directory/location and instead redirected to [localhost...] loading the index page.

Environment is as follows:

OS: Linux
Server: Apache
Backend is developed in PHP.

To achieve this I am using the .htaccess file with following rules

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} !^localhost/
RewriteRule ^(.*)$ [localhost...] [L,R=301]

Here the redirection happens properly but the rewrite condition (RewriteCond) which is put to avoid looping to [localhost...] doesn’t seem to be working. The browser is throwing error.

I want to avoid doing this using httpd.conf file of Apache as the application will be distributed and making changes to httpd.conf will not the advisable/feasible.

Any help in this context or suggestions for alternatives will be highly appreciated.

Thanks in advance.

jdMorgan

1:21 pm on Apr 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteRule / http://%{HTTP_HOST}/ [R=301,L]

or to simply deny access and use a custom 403 error page to inform the visitor what has happened (which is a good idea):

RewriteRule / - [F]

Note that if you move this code to httpd.conf or another server config file, a more-complex pattern of
 ^/[^/]*(/[^/]*)+$ 
will be required.

Jim