Below a mod_rewrite rule I cobbled up from a sitepoint tutorial. This is in .htaccess file at my shared hosting account.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^my_domain.com/live/.* [NC]
RewriteRule ^(.*)my_domain.com/(.*)$ $1my_domain.com/live/$2 [L]
The goal of this rewrite rule is to redirect all requests to a sub-directory. In effect this is to create a new Apache root directory.
Why? My shared host has a problem with me mucking about below my designated root folder. I want to create this rewrite for security reasons. Completely prevent anyone using a browser to access below the named directory. (Contrary to what it seems some others use this technique for–pay for one host account and use it as a domain riseller site)
PS. I added extra space to make the rules easier to read.
You do not understand Apache’s variables. {HTTP_HOST} contains the domain name (without the /) and {REQUEST_URI} contains everything from AFTER the / through the file extension (i.e., not the query string or page anchor). # is the demarkation for the page anchor (which would follow) and ? does the same thing for the {QUERY_STRING}.
IMHO, your goal is merely to add more crap in the URI and, by not making that visible in the URI (with R=301 flag), you’re making the relative links miss their mark by one directory level (browsers will be requesting files relative to the real DocumentRoot). Frankly, it’s just not worth the effort and trouble you’re going to.
If all you need is a secure directory (for php scripts), use mod_rewrite to prohibit all access to the secure directory (RewriteRule ^secure/ - [F] will do that for you).
FWIW, multiple domains are generally hosted with a main domain and the “addon domains” actually being treated as subdomains of the main domain. From there, it varies by host but I have … domains for my clients on my VPS server doing just that and (unless I have them use my shared secure server certificate) noone knows that they are anyone’s subdomains. Further, I rarely utilize the directories above my main DocumentRoot for anything (because of Apache’s strength in keeping files out of the hands of children (script kiddies).
Thank you for explaining the server variables. Though in my humble opinion this information is not very clear at Apache’s site. Using only the RewriteRule (without RewriteCond) this page explains the matching pattern:
“In VirtualHost context, The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. “/app1/index.html”).”
And a few lines later,
“If you wish to match against the full URL-path in a per-directory (htaccess) RewriteRule, use the %{REQUEST_URI} variable in a RewriteCond.”
And of course you’re still correct and the documentation is also correct! After your reply I reread the section. The phrase “full URL-path” is unfortunate wording with “full URL”. What Apache is doing is actually more sophisticated then it seems by the server variable’s name.
The server variables are also listed on this page, but without definitions in the clear manner which you expressed the difference between HTTP_HOST and REQUEST_URI.
–
A side note-- I’m not a big fan of such expressive words such as “crap” when explaining what someone else is trying to do. Though, it does remind me o the “good 'ol days” when IT was “god”.
Sorry for “crap” but please realize that I’ve been in this forum for years and see the same things over and over again. I’ve provided a volume of information about how to use mod_rewrite in a web page on my website (linked below as tutorial Article) and have translated that to both a SitePoint article and the first sticky thread in this forum. After all that, so see someone take an “uneducated” (sorry for that, too) flyer at mod_rewrite with both how-to and sample code at their fingertips test one’s reserve.
Thank you for continuing to read the first response, though, and I hope that it’s helped you to understand what’s going on in the mod_rewrite world. If that whet your appetite, please have a look at the tutorial Article as it is a “data dump” which is presented as a logical evolution of developing mod_rewrite code AND presents code for more than a dozen situations with explanation.