Is there a "current directory" variable in .htaccess RewriteRule?

Is there a "current directory" variable in .htaccess RewriteRule? - .htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, , , , .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :


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?


Solution :

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.


Additionally, if you would like to do some further testing, give the htaccess tester tool a try. It allows you to specify a certain URL as well as the rules you would like to include and then shows which rules were tested, which ones met the criteria, and which ones were executed.

Comments

Popular posts from this blog

Rewrite in Mediawiki, remove index.php, .htaccess

.htaccess rewrite wildcard folder paths from host

Using .htaccess to set a cookie and 301 redirect