Forum Moderators: phranque
Following is relevant information and some questions. Maybe someone can spot what I am failing to see or address.
Thanks for checking in....
I am on shared hosting (as a reseller), apache running on unix (maybe linux) recently migrated to php suexec, and we use whm and cPanel.
I don't care to rely on cPanel so much, but I believe that in order to write a vhosts configuration (or config changes) into the server file, if that's what's desired, one possibly must use this to do it. I am uncertain if cPanel checks the dns record for changes and subsequently auto generates vHosts entries, but it may do so (?anyone?). Reason I bring this up is I hear-tell that cPanel rewrites the conf file(s) each day on a regular basis (aside from rewriting on the fly as new changes are introduced manually from within cPanel), so I figure it must be checking on things periodically and making adjustments accordingly. I know it sees redirects that I write up and it applies some of those to its redirects listing, but I believe this is nothing to do with the conf files (or does it?).
My procedure typically is...
I setup subdomain in DNS record from within whm.
I setup the rewrites in .htaccess in the root directory of the hosted site.
Wala - all is good.
In this case I am uncertain as yet if cPanel scans and realizes the subdomain at some point and then adds it to its own list of subdomains, and/or does anything further with that info/record.
OR
I setup a subdomain from within cPanel.
I setup the rewrites in .htaccess in the root directory.
I check dns records for the subdomain entries injected by cPanel.
Wala - all is good.
(If cPanel generates any rewrites in htaccess files anywhere I remove them, preferring to rely on my own.)
-------------
It didn't seem to matter which way I approach it, that is, for all but the one subdomain configuration that won't work no matter what I do.
Also note that, I took one that worked and the one that doesn't, emptied the directories of all but a single html document (hello world type of thing), proceeded to transpose the relevant directory names (via renaming them) to test if it was something to do with whatever may be installed in the uncooperative directory group, but it made no difference. Only the one (referenced in section 3 below) ever fails... with either looping error from the server or a 404-file not found.
Some settings to note are...
Options +FollowSymLinks -MultiViews (tested variations on this)
RewriteEngine On
#RewriteBase / (tested on/off, ..don't need it on)
UseCanonicalName Off ...more on this below...
I am aware of clearing cache and cookies and dns cache (meticulously) while testing, and also any relevant server caches created by installed applications.
-------------------------------
Regarding UseCanonicalName directive...
I noticed in the requested copies of vHosts settings (from my host) that UseCanonicalName Off appeared in the working items, but not in the failing one, which I found to be strange. When I asked about this the host told me that it is off be default anyway and he didn't know why it would even be appearing in there for any of the subdomain entries. I asked them to place it in there for my 'broken' one anyway -just to humor me while I am testing, because it really seemed to be my last option - there seem to be no more potential issues, at least, I can think of.
Anyway, it didn't make any difference, and now I am at a loss of what I can possibly do next.
Per this last item... I think UseCanonicalName can only be set in vHost in, say httpd.conf or *.conf, but I am interested if I can set this directive in php.ini? Since we are using php suexec I think I should be able to do this, however, I am uncertain if the server needs a restart or how exactly this would work? And I don't know if I would have to write the whole relevant vHost configuration out for that subdomain, or just include that setting (but I think its the former)?
------------------------
Personally, I suspect UseCanonicalName directive may be the culprit and needs to be nailed down better somehow, but I need better server access to review the changes taking place in the *.conf file(s), and I am uncertain how to obtain this. As it is now I have to submit a ticket and wait uncertain number of hours to obtain a copy, and by then too many other things could be happening (sigh).
Other questions are,
What might cause one to loop, and not the other?
And what would cause one to perchance 404 (file not found) error, when the other never does?
Following is the relevant rewrite code being used (everything is base on the section 2 code). Keep in mind I use some other code for handling www or blank prefixes, which I am not showing here.
I hope I have provided thorough enough information.
Jeez... any help would be greatly apreciated.
Cheers,
TwoHawks
##################################################
# Section 2: Testdirect Subdomain and /test_nbhforum folder
##################################################
#### REDIRECT SUBDOMAIN CALL TO A FOLDER AND DISPLAY AS SUBDOMAIN####
#### Calls to TESTDIRECT.example.COM = contents /TESTFORUM/ ####
RewriteCond %{REQUEST_URI} !^/test_nbhforum/.*
RewriteCond %{HTTP_HOST} testdirect\.example\.com [NC]
RewriteRule (.*) /test_nbhforum/$1 [L]
#### REDIRECT CLIENT SUBDIRECTORY REQUESTS TO SUBDOMAIN (VIA ROOT HTACCESS)####
#### Non-servr Calls to /TESTDIRECT = TESTDIRECT.DOM.COM
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /testdirect/
RewriteRule ^(.*)$ "http\:\/\/www\.example\.com\/test_nbhforum" [R=301,L]
#### Non-servr Calls to /TEST_NBHFORUM = TESTDIRECT.DOM.COM (as defined in #3 above)
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /test_nbhforum/
RewriteRule ^test_nbhforum/(.*) http://testdirect.example.com/$1 [R=301,L]
################################################################
##### Section 3: NBHealers Live Section #####
################################################################
#### REDIRECT SUBDOMAIN CALL TO A FOLDER AND DISPLAY AS SUBDOMAIN####
#### Calls to NBHEALERS.example.COM = contents /NBHFORUM####
RewriteCond %{REQUEST_URI} !^/nbhforum/.*
RewriteCond %{HTTP_HOST} nbhealers\.example\.com [NC]
RewriteRule (.*) /nbhforum/$1 [L]
#### REDIRECT CLIENT SUBDIRECTORY REQUESTS TO SUBDOMAIN (VIA ROOT HTACCESS)####
#### Non-servr Calls to /NBHEALERS = NBHEALERS.DOM.COM (as defined in #3 above)
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /nbhealers/
RewriteRule ^(.*)$ "http\:\/\/www\.example\.com\/nbhforum" [R=301,L]
#### Non-servr Calls to /NBHFORUM = NBHEALERS.DOM.COM (as defined in #3 above)
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /nbhforum/
RewriteRule ^nbhforum/(.*) http://nbhealers.example.com/$1 [R=301,L]
[edited by: jdMorgan at 7:29 pm (utc) on Nov. 1, 2008]
[edit reason] Please use example.com [/edit]
I figured it might be helpful if I also post the relevant vHosts directives referred to above.
To remind, note how the second one has the directive UseCanonicalName, set to Off, while the first (failing) one does not.
After observing this I requested my host add that directive (+off) into the first one, and copy me on the file again so I could see it for myself, which they did oblige me. Testing still failed, but because I had to wait almost 12 hours to get back to it for that testing I am not confident the setting stayed in there and was present at that time. Notwithstanding, the host reassures me that this is set to off by default, but could not explain why it then would even appear in any of my vHost listings.
I don't knwo if there's a way to instruct cPanel to include and/or switch that on/off?
My Relevant VirtualHost Directives
### THE ONE THAT FAILS ###
<VirtualHost thisIPaddy:80>
ServerName nbhealers.domain.com
ServerAlias www.nbhealers.domain.com
ServerAdmin webmaster@nbhealers.domain.com
DocumentRoot /home/user/public_html/nbhealers
<IfModule mod_suphp.c>
suPHP_UserGroup user user
</IfModule>
<IfModule !mod_disable_suexec.c>
User user
Group user
</IfModule>
BytesLog /usr/local/apache/domlogs/nbhealers.domain.com-bytes_log
CustomLog /usr/local/apache/domlogs/nbhealers.domain.com combined
ScriptAlias /cgi-bin/ /home/user/public_html/nbhealers/cgi-bin/
# To customize this VirtualHost use an include file at the following location
# Include "/usr/local/apache/conf/userdata/std/1/user/nbhealers.domain.com/*.conf"
</VirtualHost>
### THE ONE THAT WORKS ###
<VirtualHost thisIPaddy:80>
ServerName testdirect.domain.com
ServerAlias www.testdirect.domain.com
DocumentRoot /home/user/public_html/testdirect
ServerAdmin webmaster@testdirect.domain.com
UseCanonicalName Off
CustomLog /usr/local/apache/domlogs/testdirect.domain.com combined
BytesLog /usr/local/apache/domlogs/testdirect.domain.com-bytes_log
## User user # Needed for Cpanel::ApacheConf
<IfModule !mod_disable_suexec.c>
User user
Group user
</IfModule>
<IfModule mod_suphp.c>
suPHP_UserGroup user user
</IfModule>
ScriptAlias /cgi-bin/ /home/user/public_html/testdirect/cgi-bin/
# To customize this VirtualHost use an include file at the following location
# Include "/usr/local/apache/conf/userdata/std/1/user/testdirect.domain.com/*.conf"
</VirtualHost>
Cheers,
TwoHawks
One problem is that you have rewrites before redirects, and that can cause internal paths to be exposed that shouldn't be. Redirects should always be coded to be before any rewrites.
^(.*)$ can be replaced with (.*) Don't escape anything on the "right" of any RewriteRule, the \/ and \. only need to be / and . without the preceding \ each time. Delete the "quotes" round that URL too.
The
.* on the end of a RewriteCond is not needed and can be omitted.
Below appear my proposed changes based on your effort to 'lead me to thinking more clearly about how to formulate a solution'. I tested it with the test scenario and its working. I have to wait until tonite to play with the other one (only can do during off/latenight hours).
I have some questions about your proposals...
1) Thank you so much for trying to impart clarity as to rewrites vs redirects. I just don't quite get some things here...
- from what I can gather, R=301 should be, or only needs to be, used at the point I wish to impose a change on the addressbar, as well as indicating a permanent relocation of a uri/url...?
- But when I have been gathering info for assigning these types of rewrites (as I have presented) I often have observed R=301 not only on the final rule directing to the subdomain.domain.com, but also on the preceding rule for pointing one folder to another (this is referencing the code I initially posted).
2. Why redirects before rewrites. I am trying to follow logically what is happening and, well it would seem logical to rewrite, say, a subdomain to a directory first, and then when its there rewrite that subdirectory uri to the desired subdomain name, then effectively redirecting it (without looping).
Would you lay down some logic to help me understand your statement about exposing internal paths? And if there's a good specific reference to read about that (of course, I am googling on this after posting, to be sure).
I guess those are my two main quandries right now.
Would you please look at the following re-do and comment. Does this reflect the understandings you were speaking to?....
##################################################
# Section 2: Testdirect Subdomain and /test_nbhforum folder
##################################################
#### REWRITE SUBDIRECTORY REQUEST TO SUBDOMAIN, REDIRECTING IT ####
#### Non-servr Calls to /TEST_NBHFORUM = TESTDIRECT.DOM.COM
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /test_nbhforum/ #!meaningful to add [NC] here?
RewriteRule ^test_nbhforum/(.*) http://testdirect.example.com/$1 [R=301,L]
#### REWRITE SUBDOMAIN CALL TO A FOLDER ####
#### Calls to TESTDIRECT.DOMAIN.COM = contents /TESTFORUM/ ####
RewriteCond %{REQUEST_URI} !^/test_nbhforum/
RewriteCond %{HTTP_HOST} testdirect\.example\.com [NC]
RewriteRule (.*) /test_nbhforum/$1 [L]
#### REWRITE SUBDIRECTORY REQUEST TO ANOTHER SUBDIRECTORY ####
#### Non-servr Calls to /TESTDIRECT/ = /TEST_NBHFORUM/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /testdirect/ #! is there a better [more elegant] way to indicate this subdirectory, i.e is ^/testdirect/ good enough?
RewriteRule (.*) http://www.example.com/test_nbhforum/ [L]
? What is the proper explanation of this last line anyway, I just don't quite get it?
- HTH -
[edited by: jdMorgan at 2:29 pm (utc) on Oct. 30, 2008]
[edit reason] example.com [/edit]
A rewrite silently accepts the URL request and using the rewrite rule fetches content from a different location within the server, without exposing what that location actually is.
The rewrite "updates" the "URL" as it is "known" within the workings of the server, and so doing a redirect after that will cause this "secret" location to be used as an input to the redirect function - meaning that the "secret" value will show up in the address bar of the browser. That's bad news.
You are now calling everything a "rewrite".
Things that end in [R=301,L] are redirects.
Things that end in [L] are rewrites.
The redirects need to be before the rewrites. You can't presently sort those out using your notes, because your notes use the wrong names.
The very last line is wrong. Delete the domain name from it if it is supposed to be a rewrite, or add R=301 if it is supposed to be a redirect.
The basic function of a server is to translate a URL to a filepath, regardless of what kind of file structure the server's operating system uses, and to return the content from that file to the requesting client.
If you think about it, that's what servers are *for* -- So that the requestor (you) don't need to know how the information you want is stored and/or organized on some other computer. You need not worry what operating system it uses, whether the machine uses a 16, 32, or 64-bit architecture or whether it's memory words are big-endian or little-endian. You need know none of that, because the URL system and servers make information retrieval OS- and hardware-neutral.
mod_rewrite can be invoked at the start of this process (as the client request arrives) to do many things. Its two most common functions are to generate a redirect response to the client to tell it to ask for the desired resource at a new URL (i.e. an external redirect), or to cause the server to use a non-default mapping for URL-to-filename translation (i.e. an internal rewrite).
By way of explanation, the server usually does this to what we call a URL:Remove the protocol and hostname/domain: http://www.example.com/foo.html --> /foo.html Add the configured DocumentRoot: /foo.html --> /home/user/public_html/example/foo.html Read contents of /home/user/public_html/example/foo.html, add HTTP response headers, and send the whole response back to the client.
mod_rewrite can take action right after the first step, and internally rewrite /foo.html to /bar.php if desired.
Or it could be used to add a different protocol and hostname onto that URL-path, respond to the client with a 301-Moved Permanently response header, and externally redirect the client off to http://www.google.com/search/q="foo.html" if you wanted to...
So mod_rewrite's two most common functions are to serve as a URL-to-URL or as a URL-to-filepath translator.
Without clearly distinguishing between URLs and filepaths, and between redirects and rewrites, we're in a bit of a muddle from the start...
I'd like to know specifically what the problem is -- "It doesn't work" tells us almost nothing.
How is the code being tested -- What are the URL 'inputs'?
What are the expected results?
What are the actual results?
How do they differ?
What (if anything) is showing in the server error log?
Can anything out of sorts be seen using a headers checker, such as the Live HTTP Headers add-on for Firefox/Mozilla?
Jim
If someone turns up already asking for the "correct" URL, only the rewrite kicks in to serve the content.
The URL that is directly "equivalent" to the internal filepath should always be protected from being directly accessed. This is done by getting the server to issue a redirect if that direct URL is requested. That redirect should take the user to the URL that you want the user to see, and this is the one that search engines will index. The URL that users see at the end of the redirect is the same URL that you should use in your links. Normal users should never see a redirect when browsing the site.
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /testdirect/ RewriteCond %{THE_REQUEST} ^/testdirect/ The answer to that is "absolutely not!"
The server variable "THE_REQUEST" refers to the HTTP client request header. It will typically appear as, for example,
GET /testdirect/foo.html HTTP/1.1
As a result, the pattern in your original code is correct if not "elegant."
This construct --checking THE_REQUEST-- is the key to redirecting only direct client requests for the "internal" server path back to the canonical URL. THE_REQUEST is tested to make sure that the client asked for the internal address directly, rather than having passed through the internal rewrite. If this test were not used, then you'd get an 'infinite' rewrite-then-redirect loop.
Jim
First let me say that I really appreciate the function and calls basics 101 you layed down. And I apologize you had to do that... as much as I have read I did not find such a simple explanation as yet to help guide my yet to bud understanding ;^)
1. The URL inputs are, or can be, either...
http://nbhealers.example.com/
http://www.example.com/nbhforum
The directories are...
a) - nbhforum contains the files location
b) - nbhealers is a blank directory being used kindof as a placeholder ...because I was unable to figure out how to setup a subdomain of one name pointing to another directory without having to create a folder with same name as that subdomain
2) The expected results are:
a) to use the nbhealers.example.com as the URL for the nbhforum
b) to redirect calls addressed to any of
- nbhealers.example.com or
- www.example.com/nbhforum or
- www.example.com/nbhealers or even
- nbhforum.example.com (but I didn't sort this out yet)
TO nbhealers.example.com ..permanently
3) The actual results so far are that:
a) for one setup with testdirect subdomain and nbh_testforum being the active directory - works flawlessly.
b) for one setup with nbhealers subdomain and nbhforum being the active directory - inconsistent failure in that inputing any of the urls affects either looping and subsequent relevant server error, or a 404 file not found (which I did specify in my initial post).
4) How do they differ... I think I lay that out fairly clearly in the initial post, i.e., there I discuss some critical points including precluding htaccess directives, the directory locations, having swapped them, and some other things, which I feel clearly indicate the problem is not to do with anything going on with, say, applications installed inside those folders or anything like that.
5) Server error logs and headers checker: I have to get back to you on that, and again, I may only be able to do that tonite when I can test the live (failing) zone and coordinate the log times with real time tests.
I remember looking in the logs initially and being frustrated by not observing anything apparently different going on between the two (testdirect group and nbh group calls). They really appeared to be the same.
For instance, no error appearing in the error log, except a missing favicon file being detected in nbhforum directory.
And in the raw access log, here's an example...
SOMEIPADDRESS - - [26/Oct/2008:12:38:51 -0500] "GET / HTTP/1.1" 200 56031 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1"
SOMEIPADDRESS - - [26/Oct/2008:12:38:51 -0500] "GET /somedirectory/file.css HTTP/1.1" 200 6884 "http://testdirect.example.com/somedirectory/file.css" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1"
...WHERE the only difference I noted before would be maybe an empty directory for nbhforum call... but I really have to retest this tonite and take a sharper look.
That's what I got for the moment.
I am going to rewrite per g1sm's suggestions and include comments explaning the intended logic better.
What I am unclear on, even with your great primer there, JD, is when I should actual be declaring only a rewrite as compared to when it should be, or better to be, the redirect. Going to think about this.
Cheers for now,
TwoHawks / HTH
[edited by: jdMorgan at 2:31 pm (utc) on Oct. 30, 2008]
[edit reason] example.com [/edit]
So then, I imagine the logic as such...
Intention: redirect calls to the forum to a subdomain, i.e., all calls for the forum shall appear as the subdomain.example.com/* location
1) ReDirect calls to /subdomainnamed_directory/.* to point to /the_actual_directory/.*
2) ReDirect calls to /the_actual_directory/.* to point to SUBDOMAINNAME.domain.com
3) ReWrite calls to SUBDOMAINNAME.example.com to /the_actual_directory/.*
?YES?
[edited by: jdMorgan at 7:30 pm (utc) on Nov. 1, 2008]
[edit reason] example.com [/edit]
<added>If you are redirecting, include the domain name in the redirect - fix it at the same time as everything else the redirect is doing.</added>
A redirect [R=301] is used to force requests for the wrong URL, to be corrected in the browser to the "correct" URL.
A rewrite is used to connect a URL request for a URL you want the user to continue to see, with an internal location inside the server that is different to what the URL suggests it would be.
.
File is at /xyz on the server, but I want the user to see it as www.domain.com/keyword on the web.
Links on the site need to point to www.domain.com/keyword as it is links that "define" URLs.
Redirect external requests for /xyx to www.domain.com/keyword [R=301,L] so that any www or non-www request for /xyz cannot directly pull content.
Redirect, site-wide, all non-www requests to www.domain.com [R=301,L] (because you don't ever want any non-www URL to directly return content).
Rewrite requests for /keyword to /xyz to fetch the content from /xyx - without revealing that the content is coming from /xyz.
2) ReDirect external requests to /the_actual_directory/.* to point to SUBDOMAINNAME.domain.com/.* (use [R=301,L]) (this is the essential redirect, and should come before rewrites, according to the recommendations for not exposing internal paths)
3) ReWrite requests to /subdomainnamed_directory/.* to point to /the_actual_directory/.* [L](does using [L] here then allow the above rule to kick in after?) (do this because the directory exists for now, but I would prefer a solution that didn't require that - but lets address that issue later and let me get the basics for this situation down)
4) ReWrite requests to SUBDOMAINNAME.example.com to /the_actual_directory/.* [L](use [L], as in #3?) (I mentioned earlier that this was unclear to me because it seems I have always observed this to be applied a full redirect, so I am uncertain where my understanding is failing here. I don't think this will work.)
I so appreciate you guys walking it through like this - I am so thirsty for the education to be "getting it". I am grateful you are willing to drop down to "rewriting 101" to help me obtain a proper grasp ;^)
HTH
[edited by: jdMorgan at 2:32 pm (utc) on Oct. 30, 2008]
[edit reason] example.com [/edit]
Am I to assume that either 3 or 4 will apply to the incoming URL request? If it is both, then there's an error in thinking there, you cannot and shouldn't chain rewrites.
I am also thinking that some of this applies to the configuration block for the domain and some applies to the sub-domain.
I have to admit, I haven't got all brain cells firing at the moment. It's 8.30 pm Sunday evening in the UK.
g1sm said: "Am I to assume that either 3 or 4 will apply to the incoming URL request? If it is both, then there's an error in thinking there, you cannot and shouldn't chain rewrites."
------
It could and should apply to incoming requests. So I was thinking, shouldn't this also be a 301 redirect to the full url, i.e...
3) ReDirect requests to /subdomainnamed_directory/.* to point to http://www.example.com/the_actual_directory/.* [R=301,L]
...which brings me back to my posting at 10:06 am on Oct 26, 2008 above ;^) (wish these posts had numbers to refer to.)
[edited by: jdMorgan at 2:32 pm (utc) on Oct. 30, 2008]
[edit reason] example.com [/edit]
The other way round, if the first doesn't apply then the more general one will.
You need to get from "wrong request" to "right URL" in just one step for any type of wrong URL.
Yes, maybe 3 should be a redirect. Sorry, I'm not firing on all cylinders today. I haven't fully followed all the logic.
*** wish these posts had numbers to refer to ***
They do, it's the # 7 digit number below the user name.
Cheers for now,
TwoHawks / HTH
I felt it best to stay focused on the concepts, say, try and get those down as well as possible before presuming to proceed too far with any coding ideas.
Here is what I have so far. Please review and comment.
Proposed Logicistics for ReDirect/ReWrite Rules for...
Intention:
. Utilize a Subdomain for facilitating users access to a website sub-section.
Essential Requirements or Logistics:
. - Create a DNS record for any subdomain you wish to employed
. - Calls to the "intended subdomain(s)' root directory(s) themselves" must appear to the user as 'being in/at' the subdomain(s), which means that/those directory(s) are hidden to the user agent.
. - If using a method that employs a mock directory having the subdomain(s) name, that must be redirected either to the intended directory or to the subdomain url, and without 'looping'
. . . (I have only been able to make the former work. IF the latter can work, then which method is a better strategy? And how would that problem be approached? [for later]).
. - Calls to the subdomain url display contents of the intended directory.
. - Good to deal with absent "www." prefixes, or "www." prefixes that may be addended to any subdomain calls.
Flow Plan:
-- Initially Create a DNS Record for the SubDomain(s) desired
. 1) ReDirect external 'client' requests to /the_actual_directory/.* to point to http://SUBDOMAINNAME.example.com/.*
. 2) ReDirect external 'client' requests to /subdomainnamed_directory/.* to point to http://www.example.com/the_actual_directory/.*
. . . (I think that if you tried to direct this to the subdomain instead that you may not be able to deal with the looping issue [at least not with the simple style of codiing being discussed here] - is this correct?)
. 3) ReDirect external 'client' requests 'not having a subdomain' to http://www.example.com (wherein the www is injected)
. 4) ReDirect external 'client' requests having www. prefixes attached to subdomain.example.com requests to their relevant subdomain.coms. I.e, ...remove "www." from any subdomain requests.
. 5) ReWrite calls to http://SUBDOMAINNAME.example.com/.* to /the_actual_directory/.*
Notes on Placement/Order: Addressing issue of having multiple SubDomains with relevant redirects
. a) First, All ReDirects of relevant directories to subdomain styled urls, a la #1 above, are grouped together first,
. b) Then ReDirect calls to directories having the same subdomainname(s) (#2) to their respective intended directory(s),
. c) Then redirect requests having no subdomain in the url (#3) to www.example.com,
. d) Then place ReDirects for removing 'www.' from subdomain requests (like www.subdomain.example.com), as in #4,
. e) Finally, rewrite the subdomain url(s) with their intended directory (#5)
I could only logically arrive at 1 rewrite, at the end. All others appear to require bonifide redirects.
Does the foregoing adequately resemble the intended understandings and logic being shared here?
Can't say thank you enough for going through this with me ;^)
Cheers,
TowHawks / HTH
[edited by: jdMorgan at 2:56 pm (utc) on Oct. 30, 2008]
[edit reason] example.com [/edit]
URLs exist "on the web" and filepaths exist "on the server". Mod_rewrite exists to sort out connecting a URL request to a server filepath, or else telling the browser to request another URL because the one asked for isn't correct.
And, you have one thing absolutely right. Defining exactly what you want to do - before you try to code it - is crucial.
More...
While I was testing I observed a glitch that made me think I had to rearrange these, but I think it was caching because it doesn't make sense that it would matter, i.e., I think that rule 11 would be called whether appearing before or after rule 10 regardless -- isn't this so?
Btw, that little thing on the www's actually helped a lot.
And JD., priceless that LiveHeaders addon - I'm loving that!
Cheers,
TwoHawks . HTH
[edited by: jdMorgan at 2:57 pm (utc) on Oct. 30, 2008]
[edit reason] example.com [/edit]
##############################################################
# Section 2: Testdirect Subdomain and /test_nbhforum folder with www prefix management
##############################################################
#### 2.REDIRECT NON-PRIME SUBDIRECTORY REQUESTS TO THE PRIME SUBDIRECTORY ####
#### Non-servr Calls to /TESTDIRECT/.* = http://www.example.com/TEST_NBHFORUM/.*
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /testdirect/
RewriteRule (.*) http://www.example.com/test_nbhforum/ [R=301,L]
#### 1.REDIRECT PRIME SUBDIRECTORY REQUESTS TO SUBDOMAIN ####
#### Non-servr Calls to /TEST_NBHFORUM = TESTDIRECT.DOM.COM
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /test_nbhforum/
RewriteRule ^test_nbhforum/(.*) http://testdirect.example.com/$1 [R=301,L]
#### 3.REDIRECT IF NO SUB-DOMAIN (inject prefix www.) ####
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)$ [NC]
RewriteRule ^(.*)$ http://www.%1.%2/$1 [R=301,L]
#### 4.Remove "www." from any subdomain requests ####
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com [NC]
RewriteRule (.*) http://%1.example.com/$1 [R=301,L]
#### 5.REWRITE SUBDOMAIN CALLS TO THEIR RELEVANT FOLDERS ####
#### Calls to TESTDIRECT.example.com/.* = contents /TESTFORUM/.* ####
RewriteCond %{REQUEST_URI} !^/test_nbhforum/
RewriteCond %{HTTP_HOST} testdirect\.example\.com [NC]
RewriteRule (.*) /test_nbhforum/$1 [L]
===================================================
How am I doing?
[edited by: jdMorgan at 2:59 pm (utc) on Oct. 30, 2008]
[edit reason] example.com [/edit]
Server Access Log relevant extract during error period:
68.238.65.243 - - [27/Oct/2008:03:04:05 -0500] "GET / HTTP/1.1" 404 4399 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1"
68.238.65.243 - - [27/Oct/2008:03:04:14 -0500] "GET / HTTP/1.1" 404 4399 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1"
Relevant LiveHeaders:
(Sorry, lost 'em)
I note how there's no directory/file coming up in the log, only a forward slash (after GET).
After removing the subdomain 'nbhealers' from within cPanel, which I am sure auto-writes a vHosts entry in the server httpd.conf file when using it to add a subdomain (listed a number of posts back), and removes the vHosts entry when deleting the subdomain; I then re-entered the DNS Record manually from within the reseller whm panel ...then the site started working as expected.
Mind you I did this exactly before, but it did not work then.
Here is the subsequent Server Access Log (relevant extract):
68.238.65.243 - - [27/Oct/2008:03:10:57 -0500] "GET /index.php HTTP/1.1" 404 4399 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1"
The 'live' header doesn't appear to tell much. In fact, both this and the one before it (when the error was there) seemed fairly identical excepting for the 404 file not found error instead of the 200 OK response
--relevant extract of live header--
http://nbhealers.example.com/
GET / HTTP/1.1
Host: nbhealers.example.com
HTTP/1.x 200 OK
=============================
It would seem safe to assume that the vHost's entry that assigns an alias and document root for 'nbhealers' which points to the directory with the same name must be the thing I am dealing with here. I am certain cPanel removed that vHosts entry upon deletion of the subdomain.
So what I really want to know is why I failed to be able to make things function in this situation? I mean all that is happening is a call to the subdomain is being redirected to its subdomainnamed folder. I would think that subsequent rewrites can handle things from there, no?
Maybe I just couldn't figure out the correct approach to do it, or maybe it cannot be done because the alias imposes a ruling?
<EDIT ADDED HERE>
I am thinking that if the server has a directive to send calls for subdomain.domain.com to a particular folder that no matter what you do after that, if you have to redirect back to that url (which you do if you wish to display it, eh?), the server is going to then take that and send the call back to the subdomain folder. I.e., it seems the server would never allow rewriting that subdomain url to the addressbar with those vHosts entries, because it has to handle it, yes?
Can we speak to this now? Actually, can you folks?
Thanks,
TwoHawks / HTH
[edited by: jdMorgan at 3:00 pm (utc) on Oct. 30, 2008]
[edit reason] example.com [/edit]
Live HTTP Headers is still useful in that it tells you whether you got a 404, a 200, a 301 followed by something different, or a 301 followed by another 301 followed by another 301...
One thing, if this stuff is going in .htaccess then it needs to be in the .htaccess for the root of the domain that it applies to.
So, stuff about example.com/folder URL requests goes in example.com/.htaccess or in example.com/folder/.htaccess if you want (but be sure it can't interfere with subdomain.example.com requests).
Stuff about subdomain.example.com URL requests goes in example.com/folder/.htaccess which is the exact same physical file as subdomain.example.com/.htaccess
That is, stuff in example.com/.htaccess cannot control requests for subdomain.example.com URLs because the .htaccess is physically above and outside the server root for that subdomain.
Yeah, this gets confusing.
[edited by: jdMorgan at 3:02 pm (utc) on Oct. 30, 2008]
[edit reason] example.com [/edit]
So anyway, no, there was no .htaccess redirect being generated that was interfering, only that auto-generated entry in vHosts.
Does it seem logical that a directive there such as I have indicated would be practically non-overrideable?
So far, my pseudo-understanding (meaning uncertain) is that using vHosts is better if you have that kind of access. That it can be more efficient for apache (less server overhead). Finding that out I figured what the heck, I'll setup a test cPanel generated vHost, which I discovered allows me now to set the DocumentRoot variable (didn't do that before afaikt) and then see what solution may present itself.
Cheers,
HTH
However, for mod_rewrite code, there can be huge advantages. This is because when you restart the server, the mod_rewrite code is compiled into machine code for later execution on a per-request basis. But in .htaccess, the mod_rewrite code must be interpreted from as-typed code into a series of calls to pre-coded routines. This is nowhere near as fast or efficient as pre-compiling, so the code in the config files will run much faster.
Jim
Will someone please check me on this (this is one followup to tightening up the above code, which I am going to focus on for now, and then later tackle the 'alternative' with vHost setup via cPanel)...
First...
I think this Condition is Checking the request with "/test_nbhforum" appearing right after the domain.com portion of the url, but would someone please read it to me proper?....
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /test_nbhforum [NC]
Then...
RewriteRule ^test_nbhforum(/?$¦.*/$¦.*/(.*)) http://testdirect.example.com/$2 [NC,R=301,L]
I think this rewrite rule says something along the lines of...
match any of the following...
1 - begins with test_nbhforum
2 - ending with one or no slash after, or
3 - followed by any number of characters and then ending with a slash (like test_nbhforum.../ , or
4 - followed by any number of chars and then a slash, "and then any characters"
5 - placing the items 2-4 into $1, and placing only quoted item in #4 into $2
6 - redirect action is the root url plus a slash plus $2 variable contents
examples:
1.http://www.example.com/test_nbhforum = http://testdirect.example.com/
2.http://www.example.com/test_nbhforum/ = http://testdirect.example.com/
3.http://www.example.com/test_nbhforum_anything_here... = error 404 file not found
4.http://www.example.com/test_nbhforum_anything_here.../ = http://testdirect.example.com/
5.http://www.example.com/test_nbhforum/edit.php?id=145 = http://testdirect.example.com/edit.php?id=145
6.http://www.example.com/test_nbhforum_anything_here//edit.php?id=145 = http://testdirect.example.com/edit.php?id=145
I could not yet figure a way to do scenario #3 because of !not usage limitation to only beginning of argument for Condition or Rule.
Comments welcome... how'd I do with this part?
[edited by: twohawks at 8:40 am (utc) on Oct. 29, 2008]
[edited by: jdMorgan at 7:31 pm (utc) on Nov. 1, 2008]
[edit reason] example.com [/edit]