2

I'm trying to find a way to dynamically change the root folder of a site. I currently set the root folder with the following:

RewriteCond %{DOCUMENT_ROOT}/foo/bar%{REQUEST_URI} -f
RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/foo/bar%{REQUEST_URI} [QSA,L]

So if a user visits http://example.com it's pulling from /foo/bar. However I would like to be able to change /foo/bar to any other random directory of my choosing with PHP.

This is done is because the root folder of the site can be changed quite regularly for various reasons.

I could just write a PHP script that will replace /foo/bar in the .htaccess file but that's not a wise decision to give PHP write access to .htaccess. Ideally I would like to have a simple .txt file that can be included to pull in /foo/bar and then PHP can write that .txt file.

Is this possible, or is there any other way to get this done? Essentially the thing that matters is that PHP can safely change the root folder without a Apache restart.

5
  • If you include a txt file in .htaccess and give PHP permission to change the txt file, isn't that the same as giving PHP permission to write to .htaccess? Commented May 21, 2013 at 17:17
  • Also: Is the evaluation of site root by PHP happening with every page load, or is something else triggering it? Put differently, can you get away with an Apache configuration reload (specifically, forcing Apache to re-evaluate environment variables) if not a restart? Commented May 21, 2013 at 17:19
  • There are other rules in the .htaccess file so it's not giving it full access. Commented May 21, 2013 at 17:21
  • Sure, but you could easily override those directives in the txt file if the evaluation order allows it. Commented May 21, 2013 at 17:22
  • Yes you can however the txt file would be at the end of the file. My goal is not to get into a security debate, just how to solve the problem the best way possible. Commented May 21, 2013 at 17:34

1 Answer 1

1

You might want to look into RewriteMap, but actually, I'd make a symlink, and point it to the proper place.

Sign up to request clarification or add additional context in comments.

2 Comments

I did look at RewriteMap but it will not work in this scenario.
+1 for the symlink. This probably the easiest way to solve this problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.