Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule without refering to full paths

How do I tell apache to use current dir as root?

         

danielkun

12:02 pm on May 30, 2010 (gmt 0)

10+ Year Member



Hello

I have a question regarding "beginning slashes" with RewriteRule.

I often find myself writing rules like the following:
RewriteRule ^([^/]+)\.html$ /index.php?page=$1 [L]

This works fine on hosting companies although with my own local setup, this will refer to "index.php" in the root directory and so I keep having to add the full path before "index.php".

For example: "/dir1/dir2/index.php?page=$1"

What I'm wondering is, what do I have to do to my local setup (xampp) to make apache understand "/index.php" means the current dir?

Regards,

Daniel

jdMorgan

1:20 pm on May 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> What I'm wondering is, what do I have to do to my local setup (xampp) to make apache understand "/index.php" means the current dir?

The actual answer to this question is, "You have to re-write your operating system's file-handling code, and modify the mod_rewrite module and Apache's file-handlers as well.

Slash-path in Apache always means "<DocumentRoot>/path" and slash-path in your OS always means "this disk's root."

So, you'll need to do something else, such as define DocumentRoot more appropriately, or use RewriteBase, or code a variable into your RewriteRules' substitution paths, depending on what problem you are trying to solve here.

Jim

danielkun

1:35 am on May 31, 2010 (gmt 0)

10+ Year Member



Thank you Jim.

My question may not have been very specific to what I am trying to accomplish but your answer is exactly what I think I need to know ;-)
However, after having searched the web for a while I'm not really sure how to solve this problem.

When I work on websites locally I divide sites into sub-directories like the following:
localhost/web/company1/site1
localhost/web/company1/site2
localhost/web/company2/site1

and therefor when I use RewriteRule(s) I have to specify the full path like in my example above. (/dir1/dir2/index.php?page=$1) When I upload the site to the domain though, I have to edit all .htaccess files for that site in *every* sub directoy. ('edit' here means removing the above "/dir1/dir2" from the path)

Is it possible to set a variable in the root of the site where I would add the local path (/web/company1/site1) and have this variable included in all .htaccess files in sub directories? And when I upload the site to the server just rewrite this path once and not in every .htaccess file like I'm doing now?

Regards,
Daniel

EDIT: The easiest would be if it's possible to set a global variable in the root .htaccess with the path to be used and then have .htaccess files in subdirs refer to this variable. I still haven't figured out how this is done though.

danielkun

3:58 am on May 31, 2010 (gmt 0)

10+ Year Member



I found another thread (http://www.webmasterworld.com/forum92/2231.htm) which is similar to what I'm trying to accomplish.

But as Jim wrote in this tread:
How this may affect you is if you use two different modules to set and test variables. The code

setEnv foo bar
... (additonal logic)
RewriteCond %{ENV:foo} ^bar$
RewriteRule ^.*$ /isbar.php?why=foo

may or may not work, depending on whether mod_setenv or mod_rewrite processes the code first. A better solution might be:

RewriteRule .* - [E=foo:bar]
... (additional logic)
RewriteCond %{ENV:foo} ^bar$
RewriteRule ^.*$ /isbar.php?why=foo

"setEnv foo bar" isn't working for me. And the second option only works if I place the code in *every* .htaccess file. I was hoping to be able to write the global variable in the root .htaccess file only.

jdMorgan

1:35 pm on May 31, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It sounds to me like you'd be a lot happier handling your per-client development sites using individual VirtualHost containers in the server config file(s), rather than using mod_rewrite with variables embedded in the code.

The latter method is workable on servers where the Webmaster does not have access to the server configuration, but it takes a lot more work -- and it leaves the .htaccess code bloated with code that does not benefit the client.

Look into setting up separate vHosts for each of your clients, which avoids any customization requirements at the deliverable level. Once you've got one "client site" working, you can simply copy-and-paste the vHost definition to add new ones.

Code (scripts, etc.) and templates can be shared between client vHosts using mod_alias in Apache and/or symbolic links in the OS'es filesystem.

Jim