Jump to content

Getting Mod_Rewrite To Work


Recommended Posts

I am trying to come up with a way of redirecting all requests for files in the directory http://mydomain/swchoir/<filename> to the file with the same name (and the same GET parameters if present) in the directory /home/myaccount/build00001/public_html/swchoir/<filename>.

My end objective is to allow the server to contain several different builds and for people who have the necesary permissions to be able to choose which one they want to look at, so for example I could have one build that is on public release, another that is in QA, and a third which is in development. Ordinary users would only be able to see the public release one. QA people would be able to see either the public release or the QA build. And developers would be able to see all of them.

As a step towards this, I am trying to develop some rewrite rules that will send _all_ users to build00001. The lines I have added to the .htaccess file in /home/sylvest/public_html are:

SetEnvIfExpr True build_num=00001
Rewrite Rule ^swchoir/([0-9a-z/\.?&_=-]+)$ /home/myaccount/buildbuild_num/public_html/swchoir/$1 [NC, L]
php_value include_path ".:/home/myaccount/buildbuild_num/include"

Now when I try to access a page I get an internal server error.

Please can you suggest what is wrong with my rule, or how to best get this to do what I want? In particular I'm not sure how to use the Environmental Variable build_num that I have tried to define. I'm not sure whether "SetEnvIfExpr True" is valid, and I'm not sure whether "sylvest/buildbuild_num/public_html" is a valid way of using the variable. Seems unlikely, but I'm not sure whether it needs some kind of quotes to indicate that it is a variable name, and if so, what kind.

If you know a better way of doing the whole job, please tell me that too. It seems odd to me that something like this doesn't already exist somewhere, since it surely must be a commmon need.

Thanks - Rowan

Link to comment
Share on other sites

Hi Rowan,


We had already discussed this via our ticket system but let me confirm once again. SetEnvIfExpr can be used to match a url, a pattern matching a portion of url and but the this rule you have defined is unnecessary with your requirement as you are just trying to redirect all the incoming requests to subdirectory without any specific condition. Second Rewrite rule which is confusing. As we already discussed you are trying to redirect a url or a directory to a particular directory which is on publicly unavailable location via your main url or sub url. Also there is a syntax error in your redirect rule but that can be fixed but again problem is redirecting url to a path which is publicly unavailable.

Link to comment
Share on other sites

I have found many discussions on the web around this topic. Some say that a rewrite rule assumes that the redirect path is a file system path if the first element of the path (/home in this case) exists at the root of the file system (which it obviously does in this case). Otherwise it assumes that it is relative to the Document Root. Other people imply that redirect to a file system path is not permitted for .htaccess files for security reasons, but that you can use an Alias as a work around.


Is it in fact possible to write a RewriteRule that will work in an .htaccess file on a TCH server to redirect to a file system path? How exactly do I do this?


If not, is it possible to use an Alias (which would presumably point to a file system path from a web URL)? If so, how do I do this?


You may ask - why do I want to do this? The answer is that I prefer to keep all the files that I include or redirect to in non web accessible places, to minimise the chance that someone can deduce or guess the names of these files, and then download them, and see the inner workings of my web site, security system etc,. thus compromising my security.


I find it difficult to believe given all the things that mod_rewrite can do, that it can't do this...


Thanks - Rowan

Link to comment
Share on other sites



Thanks for your reply.


Why is it that I can't do an internal rewrite to a location above public_html?


Here is an exerpt from http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule

The Substitution of a rewrite rule is the string that replaces the original URL-path that was matched by Pattern. The Substitution may be a:

file-system path Designates the location on the file-system of the resource to be delivered to the client. Substitutions are only treated 
as a file-system path when the rule is configured in server (virtualhost) context and the first component of the path in the substitution 
is exists in the file-system

So I guess this is saying that what I'm trying to do won't work in an .htaccess file. But is there a way that I can make it work in my Virtual Host config file?


Surely there cabn;t be any fundamental reason why I can;t redirect to a file system diretory above public_html? I can after all include any of these files, which is pretty close to the same thing?


Thanks - Rowan

Edited by sylvest
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...