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.
LVL 1
Vipin KumarSr. Network EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lucas BishopClick TrackerCommented:
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

0
Vipin KumarSr. Network EngineerAuthor Commented:
@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.
0
Lucas BishopClick TrackerCommented:
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.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Vipin KumarSr. Network EngineerAuthor Commented:
@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.Output
0
David Johnson, CD, MVPOwnerCommented:
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
0
Steve BinkCommented:
Try using Notepad instead of Wordpad.  As Lucas Bishop mentioned, not all editors are created equal.

The directives suggested to you look valid.  Take a look at the Apache access/error logs to see what is being reported for the failed request.  Also:

1) What version of Apache are you using?
2) Please post your VirtualHost definition for this site.
3) What is the DocumentRoot for this site?
4) What is the full path to the /website folder?

As David Johnson pointed out, you should remove the Deny directive to allow the rewrites to take effect.  I doubt that is the cause of the internal error, though.  Leaving it in should result in a "Forbidden" response instead.

Finally, some alternative rules you could try.  I doubt these will work until we resolve the error you are already receiving:
RewriteEngine On
RewriteBase /

# the original
RewriteRule ^include/(.*)$ /website/somefile.name [L,R=301]

# alternative 1, minor syntax change
RewriteRule ^/?include/.*$ /website/somefile.name [L,R=301]

# alternative 2, using Forbidden instead, which isn't what you want per the original question
RewriteRule ^/?include/.*$ - [F]

Open in new window

0
Vipin KumarSr. Network EngineerAuthor Commented:
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.
0
Steve BinkCommented:
The error you posted indicates mod_rewrite is not being loaded by Apache.  You'll need to enable that module.  See this article for a basic tutorial in how this is accomplished.

You should also take a brief look at using the Override directive to enable your .htaccess files to do what you need.  Most mod_rewrite directives require the FileInfo override permission.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vipin KumarSr. Network EngineerAuthor Commented:
Will try and get back
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Web Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.