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 8546261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:58:34+00:00 2026-06-11T12:58:34+00:00

We are using an .htaccess in combination with anchor tags to serve files and

  • 0

We are using an .htaccess in combination with anchor tags to serve files and conceal the server directory structure.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /_docu/clients/$1/$2/$3.pdf [NC,L]

For example, all files are stored under /public_html/_docu/clients/ and inside that folder are listed all the clients, and then under each client their projects. However, an anchor tag for a file would read only:

http://mydomain.com/client-name/proj-name/docname.pdf

(the /_docu/clients/ being omitted – there is a good reason for this). The above .htaccess grabs the client-name, proj-name and docname and serves it from the correct folder:

http://mydomain.com/_docu/clients/client-name/proj-name/docname.pdf

whilst preserving in the address bar the incorrect (concealed) directory structure.

I wish to handle the error condition of a document not existing. This should never happen, but it could. Can anyone suggest a way of dealing with this? Can something functionally akin to “if fileexist($1/$2/%3.pdf)” be somehow constructed in an .htaccess file?


EDIT:

Delayed response as JL’s answer below required research and experimentation. Thanks, Jon, for the gentle push in the right direction but I haven’t got it to work just yet. Here’s what I tried:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# check if the requested file exists in the "_docu/clients" directory
RewriteCond %{DOCUMENT_ROOT}/_docu/clients/$1/$2/$3.pdf -f
RewriteRule ^([a-z0-9])/([a-z0-9])/([a-z0-9]*).pdf$ /_docu/clients/$1/$2/$3.pdf [NC,L]
RewriteRule ^(.*)$ /errors/404.php [L]

I thought that what that should do is:

  1. If http://mydomain.com/_docu/clients/$1/$2/$3.pdf does not exist,
  2. GoTo page http://mydomain.com/errors/404.php

Actual outcome is an “internal server error” message.


EDIT TWO:

Latest changes:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([a-z0-9])/([a-z0-9])/([a-z0-9]*).pdf$
RewriteCond %{DOCUMENT_ROOT}/_data/cli/%1/%2/%3.pdf -f
RewriteRule ^([a-z0-9])/([a-z0-9])/([a-z0-9]*).pdf$ /_data/cli/$1/$2/$3.pdf [NC,L]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^(.*)$ /metshare/404.php [L]

The problem with this one is that legitimate pages also are directed to 404.php

MESSAGE TO FUTURE READERS:

All of the above concerns were addressed in Jon Lin’s final answer. As issues were detected, he modified his answer until it was a perfect, working solution. I am leaving the above as it is because there are some good ULOs within (unscheduled learning opportunities) for those who want to compare versions.

  • 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-06-11T12:58:35+00:00Added an answer on June 11, 2026 at 12:58 pm

    You need to use a condition like this:

    RewriteCond %{DOCUMENT_ROOT}/_docu/clients%{REQUEST_URI} -f
    

    So that your rules looks ssomething like:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # check if the requested file exists in the "_docu/clients" directory
    RewriteCond %{DOCUMENT_ROOT}/_docu/clients%{REQUEST_URI} -f
    RewriteRule ^ /_docu/clients%{REQUEST_URI} [L]
    

    EDIT: Response to edit in question

    You can’t do this:

    RewriteCond %{DOCUMENT_ROOT}/_docu/clients/$1/$2/$3.pdf -f
    

    Because the backreferences for $1/$2/$3 don’t exist yet, they are matched in the groupings in your RewriteRule, which hasn’t happened yet at this point. But you can try something like this:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # check if the requested file exists in the "_docu/clients" directory
    RewriteCond %{REQUEST_URI} ^/([a-z0-9])/([a-z0-9])/([a-z0-9]*).pdf$
    RewriteCond %{DOCUMENT_ROOT}/_docu/clients/%1/%2/%3.pdf -f
    RewriteRule ^([a-z0-9])/([a-z0-9])/([a-z0-9]*).pdf$ /_docu/clients/$1/$2/$3.pdf [NC,L]
    
    RewriteCond %{ENV:REDIRECT_STATUS} !200
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /errors/404.php [L]
    

    Essentially creating a match against %{REQUEST_URI} in a previous RewriteCond then using the %N backreferences in the following RewriteCond.

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

Sidebar

Related Questions

I am using htaccess coppied from wordpress. RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule
Right now im using .htaccess to hide .htm extentions: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f
I have been using .htaccess files to redirect some of my renamed controllers/actions. Say
I'm normally used to using .htaccess files to force a domain to use www.
I want to configure a mod_rewrite rule without using .htaccess files. When I put
Is there a way to stop Apache creating error_log files using .htaccess ?
I'm having trouble using .htaccess. This is the content of my .htaccess file RewriteEngine
Using .htaccess, I'd like to redirect files that do not exist to a controller
Using .htaccess file in apache server, how can I get http://mysite/[category]/[post-title] redirect to http://mysite/[post-title]
I like using .htaccess to password protect a directory, especially since it recursively protects

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.