Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7639681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:28:52+00:00 2026-05-31T08:28:52+00:00

I have a protected folder. I want people who are logged in (via PHP

  • 0

I have a protected folder. I want people who are logged in (via PHP / WordPress) to have access to the folder and the files therein.

Those who are not logged in should be redirected via .htaccess.

Can the .htaccess rewrite condition be based off an environment variable or a server variable which I added or edited from PHP?

UPDATE:

See my answer below.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:28:54+00:00Added an answer on May 31, 2026 at 8:28 am

    After much research, I solved it.

    My folder system is setup like this:

    /file-share/users-folder-name
    

    My .htaccess file under /file-share is as follows:

    # .htaccess /file-share
    
    RewriteEngine On
    RewriteBase /
    
    # no cookie set    
    RewriteCond %{HTTP_COOKIE} !^.*client.*$ [NC]
    RewriteRule ^(.*)$ /file-share-redirect.php?q=$1 [NC,L]
    
    # cookie set 
    RewriteCond %{QUERY_STRING} !verified$ [NC]
    RewriteCond %{HTTP_COOKIE} client=([^;]+) [NC]
    RewriteRule ^(.*)$ /file-share/%1/$1?verified [NC,L,QSA]
    
    # custom 404
    ErrorDocument 404 /file-share-redirect.php?e=404&q=$1
    
    #end
    

    If the cookie is set and the file exists in the client’s folder then the client is redirected seamlessly to the requested file. The final file request is also given a url parameter to avoid a loop in redirection.

    If a user is logged but the cookie is not set I have my file-share-redirect.php file create the cookie then redirect to the requested file. The cookie created in the code below is set to expire in an hour.

    <?php setcookie('client', $users_folder_name, time()+3600); ?>
    

    UPDATE

    You can keep the cookie secure by using an encrypted cookie name and value. The cookie will only be created on systems where users log in.

    PHP’s setcookie() will even let you create a cookie that is inaccessible from JavaScript. I double checked this.

    The subfolder names will be quite complex, completely unguessable. No one will ever see the subfolder names except those with ftp access. Even those logged in will only see /_/filename.ext, without the subfolder.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a folder with multiple .aspx pages that I want to restrict access
I have a login-protected website. It's an internal application and it's not avaiable to
I have a folder for downloads on my server, i want to prevent direct
I have a page which lists all the files in a particular folder (all
Hai I want to protect a folder (protect) and the files inside that folder.
I want to create a private url as http://domain.com/content.php?secret_token=XXXXX Then, only visitors who have
I have a directory and I want to allow users which are only logged
I want to display 404 error page for that i have made error404.php file
I have a treeview and a imageList which contain 1 icon (folder.ico), I want
I have a problem with redirect to protected folder. There are two domains: domain1.com

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.