1

In a .htaccess file in the subfolder "/preview" (not in document root), I have this rule:

RewriteRule !^public/ /preview/forbidden.php [L,R]

It redirects all /preview/"something" requests that are not in /preview/public/ to the fobidden message.

However, I don't like the fact that the directory name "preview" is in the .htaccess file. I would like to copy the entire website to another folder or server simply by copying the file without having to change the .htaccess file.

So, is it possible to achieve the effect of that rule in some other way?

2
  • I started to answer your question then I became confused as to what the problem is that you are trying to solve with the regular expression. As well, the .htaccess file is a hidden so that Apache cannot serve it. You would have to be logged in and have privileges to see the file. Could you make it clearer what problem you are trying to solve with the regex? I am sure there is a simple answer for you.
    – closetnoc
    Mar 20 '14 at 15:49
  • "Redirect everyone trying to access something not in /preview/public to /preview/forbidden.php". That is, I want to bar access to everything else. My problem is that I don't want to have the word 'preview' in the .htaccess file so I can easily copy the whole application (including .htaccess) file to other folders. Mar 20 '14 at 16:10
1

Your regular expression (regex) says something like this:

If path is NOT /public then goto /preview/forbidden.php

...so that if placed in the /preview directory, anyone who goes to the /preview directory is redirected to the forbidden.php page.

Try these options:

RewriteRule !^index\.php$ forbidden.php

RewriteRule !^index\.php$ /forbidden/index.php

The first one assumes a forbidden.php in each directory while the second utilizes one forbidden.php for any directory you place the .htaccess into. I assumed that there is a valid page for each directory. If not then try this:

RewriteRule !^forbidden\.php$ forbidden.php

RewriteRule !^forbidden\.php$ /forbidden/index.php

I try and keep just one .htaccess in the root directory if possible. There are times where I do create a specific .htaccess file for a particular directory. I do this for simplicity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.