Jump to content


Photo

Getting Mod_Rewrite To Work


  • Please log in to reply
4 replies to this topic

#1 sylvest

sylvest

    Family Friend

  • Members
  • PipPip
  • 96 posts

Posted 20 April 2017 - 01:21 PM

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


 



#2 TCH-Marshal

TCH-Marshal

    New To The Neighborhood

  • Staff
  • Pip
  • 17 posts

Posted 21 April 2017 - 11:25 AM

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.



#3 sylvest

sylvest

    Family Friend

  • Members
  • PipPip
  • 96 posts

Posted 24 April 2017 - 11:57 AM

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



#4 TCH-Bruce

TCH-Bruce

    Volunteer Moderator

  • Members
  • PipPipPipPip
  • 20,131 posts

Posted 24 April 2017 - 03:50 PM

You can write rewrite rules that will work as long as the paths are correct.  You cannot write rewrites to access content in folders above public_html. In other words your content must reside in folders within public_html.


Bruce Richards
Forum Moderator
TotalChoice Hosting, Inc.
Webhosting by Total Choice Web Hosting - General Support Forum

I am a Forum Moderator. While I can assist in answering most of your hosting related questions, I am unable to answer questions about specifics relating to your account such as billing and server related issues. Should you need assistance in these areas, please contact our Help Desk or our many other options. Another good place to find answers is with our help pages, tutorials and movie tutorials.


#5 sylvest

sylvest

    Family Friend

  • Members
  • PipPip
  • 96 posts

Posted 24 April 2017 - 05:18 PM

Bruce,

 

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....tml#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, 24 April 2017 - 05:19 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users