Link to home
Start Free TrialLog in
Avatar of Vipin Kumar
Vipin KumarFlag for India

asked on

Redirect a subdirectory to specific page in root directory using .htaccess

Hi,

I have the below folder structure in my www directory.

www/website/include/client/

The website folder contains all the files that are to be accessed by the users. The include folder contains all the include files.

Currently when someone directly tries to access any file in include or client folder they are able to access them. I placed a .htaccess containing "Deny from All" in the include folder which has stopped users from accessing the files in include and client folder.

But instead of that I want them to be redirected to a file in website folder whenever someone tries to directly access the files in include folder or any subfolder that include contains.

Also, currently I am using this on localhost so kindly let me know such redirect that even later on this website moves to any domain I don;t need to make changes in the .htaccess file.

I hope, I have explained my self here.

Thanks in advance.
Avatar of Lucas Bishop
Lucas Bishop
Flag of United States of America image

Are there files inside of include or client that should be accessible to certain people?

For example, if there are legitimate reasons for the files to be accessible, you may not want to redirect every person that tries to access a file here.

In any event, you can put this code in the root directory .htaccess file to redirect all references into the include folder and below:
RewriteEngine On
RewriteBase /
RewriteRule ^include/(.*)$ /website/somefile.name [L,R=301]

Open in new window

Avatar of Vipin Kumar

ASKER

@Lucas,

When I placed your code in the .htaccess file and placed that file in the root directory it is giving me the Internal Server Error

I tried placing the .htaccess file in the include folder as well it is the same.
There may be something else in the htaccess that's conflicting with the code I gave you.

If you put the code I gave you in here, you'll see the logic is sound:
http://htaccess.madewithlove.be/

What else is in your htaccess file? Also, how are you editing the file? Some text editors will insert hidden characters that can cause issues.
@Lucas,

There is nothing else in the .htaccess file other than the code that you provided. I opened the file with WORDPAD and edited.

Also, when I tried your code at the testing site provided by you I got the same result, it is not redirecting. Screenshot of the output attached along.User generated image
I placed a .htaccess containing "Deny from All" in the include folder which has stopped users from accessing the files in include and client folder. a possible unintended consequence is the website not being able to access the include/client folders
SOLUTION
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Steve,

Here are the inputs for questions:
1. Apache Version - 2.4.9
2. Don't know how to get VirtualHost definition, although I have not defined any
3. the DocumentRoot is the /website/
4. C:\program files\wamp\www

There are no Deny directive in .htaccess file. I tried the alternative rules your provided but it is the same case. The Apache access/error logs shows the below. I tried placing the .htaccess in root folder as well the include folder but no difference.

::1 - - [24/Jun/2015:11:20:24 +0530] "GET /schweickert/include/client/add-staff.inc.php HTTP/1.1" 500 616
::1 - - [24/Jun/2015:11:18:45 +0530] "GET /website/include/client/add-staff.inc.php HTTP/1.1" 500 616

[Wed Jun 24 11:18:45.862335 2015] [core:alert] [pid 6724:tid 952] [client ::1:4850] C:/wamp/www/website/include/.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration

[Wed Jun 24 11:20:24.617159 2015] [core:alert] [pid 6724:tid 952] [client ::1:4898] C:/wamp/www/schweickert/.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration

Kindly let me know if anything else is required.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Will try and get back