homepage Welcome to WebmasterWorld Guest from 174.129.103.100
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Need .htaccess file
Can access primary domain but not subdomains
raydona




msg:4680063
 6:22 pm on Jun 15, 2014 (gmt 0)

Hi,
I have a primary domain, let's call, primaryA.com. To get into primaryA.com from browser you have to enter folderA. I have written .htaccess file that allows that:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/folderA [NC]
RewriteRule ^(.*)$ /folderA/$1 [L]

This works, allows entry into primary domain. However, if address of either sub domains, let's call subdomA.com and subdomB.com, are entered into browser I get following error messages in browser.

Not Found
The requested URL /folderA/subdomA.com/ was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

I feel the .htaccess file has to be rewritten to allow entry into folderA and into subdomains. I have no idea how to do that. Any suggestions please. I will be grateful for all help.

 

lucy24




msg:4680082
 8:25 pm on Jun 15, 2014 (gmt 0)

Let's try that again using example.com. Are you saying that

subdomain.example.com/blahblah
and
othersub.example.com/blahblah

should be accessed as-is, while only the primary gets rewritten? Then you need a RewriteCond looking at %{HTTP_HOST}.

RewriteCond %{REQUEST_URI} !^/folderA [NC]

Do not repeat not use [NC] in this situation-- or, for that matter, in almost any other situation involving [L] rewrites alone. What if someone asks for
example.com/FOLderA/blahblah
or
example.com/Foldera/blahblah
?

For that matter, why is the exception there at all? You've created Duplicate Content all over the map by allowing people to get to the same page both with and without /folder/ in the URL. For it to work, you need a preceding ruleset that looks at {THE_REQUEST} and forcibly redirects anyone who asks for /folderA/ by name. If they use the wrong casing, they can and should get a 404.

Speaking of which...

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

This Apache boilerplate means that
#1 there exists an ErrorDocument directive somewhere-- probably in a shared host's config file, with a standard name such as "missing.html", and
#2 you don't have a custom error document with the specified name.
As long as you're in there, you should remedy this situation. Custom error documents are for humans; they're part of their overall site experience.

phranque




msg:4680104
 11:58 pm on Jun 15, 2014 (gmt 0)

However, if address of either sub domains, let's call subdomA.com and subdomB.com, are entered into browser I get following error messages in browser.

Not Found
The requested URL /folderA/subdomA.com/ was not found on this server.

are you linking to subdomA.com or to http://sobdomA.com?

raydona




msg:4684699
 2:00 am on Jul 3, 2014 (gmt 0)

Hi, Sorry for the late reply but I was very busy.
lucy24
Senior Member
I wish I could understand your reply. It is too technical for me.
My point is that if I enter example.com in google search or enter http://example.com in the address bar, either way I get the error message.

[edited by: incrediBILL at 5:59 am (utc) on Jul 3, 2014]
[edit reason] Exemplified [/edit]

lucy24




msg:4684719
 4:17 am on Jul 3, 2014 (gmt 0)

either way I get the error message.

That's because your server is looking for

suba.example.com/folderA/blahblah

and it doesn't exist. If you want to be technical about it, the server is looking for something like
example.com/suba/folderA/blahblah
(because a subdomain has to exist somewhere in two-dimensional serverspace) but the effect is the same.


The solution is still to add a RewriteCond that says something like
RewriteCond %{HTTP_HOST} ^example\.com
or
RewriteCond %{HTTP_HOST} www\.example\.com
depending on which form you use for the overall domain name. If it's without www you have to include the opening anchor; if it's with www, it probably isn't essential.

But you still need to make sure people can't get to /folderA/ if they ask for it by name.

not2easy




msg:4684730
 5:14 am on Jul 3, 2014 (gmt 0)

If this is a case of having more than one domain hosted on a shared hosting account and you can only access the main domain through the /folderA/ directory as if it is not located in the root directory, make sure that the directories are where they should be.

The domains can be accessed through the main domain but not as their own sites? Am I misreading this? You don't have all these domains in the /home/user/public_html/ folder, right? I'm trying to figure out why you need an htaccess rewrite rule to access your primary domain, and why you would want to send everything to one folder. The error if you type in your primary domain suggests to me that removing the htaccess file to see if you can access it without any htaccess flie is the place to start. You can leave it there and temporarily rename it so it is simple to replace after you test without it. Sorry I can't get a clearer picture of what is happening.

lucy24




msg:4684742
 6:46 am on Jul 3, 2014 (gmt 0)

There's another point I missed the first time around.
The requested URL /folderA/subdomA.com/ was not found on this server.

Is this literally what it says? (Allowing for name obfuscation, that is.) Or does it really say
/subdomA.com/folderA/
in that order?

See, it's easy to understand the server looking for /folderA/ within any given hostname. But what's it doing, looking for /sudomA.com/ inside /folderA/ ?

I think we also need to know how the subdomains are set up. What are the physical directories involved?

raydona




msg:4686431
 5:10 pm on Jul 9, 2014 (gmt 0)

Hi, It is a virtual server. The primary domain is in the folder public_html. public_html contains four folders, one for subdomainA, one for subdomainB, a folder called cgi-bin and folderA which contains the index.php file for the primary domain.

public_html(primary domain) => cgi-bin (folder)
public_html(primary domain) => folderA (folder)
public_html(primary domain) => subdomainA (folder)
public_html(primary domain) => subdomainB (folder)

public_html also contains .htaccess file (given previously) to access folderA. I had created the two sub-domains. One of the sub-domains I can now access as I have put a ready made script in there which came with its own .htaccess file (too long and technical for me to understand). The other sub-domain I cannot access and now I'm guessing it needs a .htaccess file to reside in that domain.
I have created, as suggested by lucy24, a .htaccess file and placed it inside the second domain. I firstly tried the code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^2ndomain\.com

and then code:

RewriteEngine On
RewriteCond %{HTTP_HOST} www\.2ndomain\.com

but neither seem to be working.

Any suggestions please?

lucy24




msg:4686470
 7:49 pm on Jul 9, 2014 (gmt 0)

Preliminary test to see whether htaccess is working at all:

RewriteRule zubzub http://www.example.com/whoopdedo.html [R=301,L]

where "www.example.com" is the name of the (sub)domain you're checking, and the two filenames are pages that don't actually exist. I generally say "widget" on one side and "foobar" on the other. If you request "zubzub.html" in your browser, you should end up on your 404 page, and the browser's address bar should say "whoopdedo.html".

Verify this step and then come back.

raydona




msg:4687271
 1:26 am on Jul 13, 2014 (gmt 0)

Hi,
I've tried as you suggested but still get:
Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request......

Please advise

not2easy




msg:4687287
 3:20 am on Jul 13, 2014 (gmt 0)

I have created, as suggested by lucy24, a .htaccess file and placed it inside the second domain. I firstly tried the code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^2ndomain\.com

and then code:

RewriteEngine On
RewriteCond %{HTTP_HOST} www\.2ndomain\.com

but neither seem to be working.

Those single lines can't work alone, that is showing only a condition, but not what to do when the condition is met, so they can't do anything.

With the folder structure you described, it looks like your primary domain is supposed to be in the public_html folder, not in folderA. FolderA could only be accessed by going to http://example.com/folderA and subdomainA would be accessed by browsing to http://example.com/subdomainA which has more to do with where the files were put than with htaccess.

If you want to access subdomainA at example.com/subdomainA then the folders and pages for folderA (primary domain) would need to be in the public_html folder. Then it can be done as you described: access example.com/subdomainA - unless I got lost as to what it was supposed to do (apologies if that's the case). IF the oly thing in public_html is these folders and no other files. it would need at a minimum an index file to control it from the root.

lucy24




msg:4687296
 4:08 am on Jul 13, 2014 (gmt 0)

so they can't do anything

Empty lines in Apache have no syntactic meaning, so an orphaned condition will simply be read with the next RewriteRule, whatever it may be.

:: detour to check something ::

Surprisingly, a stray RewriteCond with no following rules at all just evaporates. You'd expect a 500 error, wouldn't you?

raydona, where are you getting the server error? Does it happen with all requests or only some? What does the htaccess say, assuming you've only got one?

If possible, look at your error logs for the affected time period and see what's up. Access logs and error logs overlap in the information they give. But problems with internal rewrites only show up in error logs.

raydona




msg:4687350
 12:37 pm on Jul 13, 2014 (gmt 0)

If you want to access subdomainA at example.com/subdomainA then the folders and pages for folderA (primary domain) would need to be in the public_html folder.

The script in the primary domain is a ready made script and the readme file for installation stipulated (for security reason or otherwise) that the folders had to reside in a separate folder. I don't want to bring out all the files and folders from folderA and put them in public_html in case it messes up the directory structure used in the coding.

If possible, look at your error logs for the affected time period and see what's up. Access logs and error logs overlap in the information they give. But problems with internal rewrites only show up in error logs.

I have looked at the error logs and this is what is given:

[Sun Jul 13 07:16:50 2014] [error] [client 86.144.218.143] File does not exist

The same error is repeated several times.

not2easy




msg:4687371
 1:44 pm on Jul 13, 2014 (gmt 0)

It sounds like your ready made script is not intended to be the home page, it assumes it is being added to an existing site. So to see results from the script's output you would browse to http://example.com/script and see the content produced by the script. It is common for this type of script to offer very detailed instructions on installation because they do need to maintain the structure they are set up for. Before you uploaded your script folders, what files were in your public_html folder? Was it only a folder called cgi-bin? If that is the case you would need a minimum of one index.html or index.php page in the public_html folder to link to the other folders.IF the script is supposed to create a full-blown website, it is the contents of folderA that belong in the public_html folder, not the folder itself. Can you view the folder's contents on your desktop to see if there is an index file in it?

raydona




msg:4688040
 12:10 am on Jul 16, 2014 (gmt 0)

Before you uploaded your script folders, what files were in your public_html folder? Was it only a folder called cgi-bin?

Yes it was only cgi-bin (apart from the two sub-domains).
Can you view the folder's contents on your desktop to see if there is an index file in it

Yes there is an index.php file

lucy24




msg:4688056
 1:18 am on Jul 16, 2014 (gmt 0)

[Sun Jul 13 07:16:50 2014] [error] [client 86.144.218.143] File does not exist

The same error is repeated several times.

Is it several times on the same request? Access logs list each external request separately; error logs show internal requests. That's why it is useful to study both.

Also, is this the same error you referred to earlier as a "server error"? Usually when people talk about server errors they mean 500-and-up. "File does not exist" is a 404 unless the non-existence is part of some larger pattern. For example: If you serve a 403, the server will go looking for a custom 403 page in the location specified by the config file. If there isn't one although the config file says it exists, then error logs will show a series of "file does not exist", but access logs-- and the human user-- will only show the 403.

Yes there is an index.php file

Is there code somewhere that tells the server to look for "index.php"? The default is only index.html. But if you are on shared hosting, it is safe to assume that .php and .htm, at a minimum, were added as options. If in doubt, you can easily test this with a made-to-order directory.

Wait, one more question. Do you currently have any htaccess file(s), anywhere, that work? They don't have to do everything as intended. Just work, somehow, in some way.

public_html(primary domain) => folderA (folder)

Does this configuration currently work? That is, can you navigate to pages in your primary domain using an ordinary browser?

not2easy




msg:4688059
 1:55 am on Jul 16, 2014 (gmt 0)

The server default is to look in the root directory for those files and there is no index in the root directory, only the folders named above. There are no pages in the root directory so the first rule in the OP makes sense. If you browse to www.example.com there's no index page, only folders. The primary domain is one of those folders and the scripts are in another and there are a few subdomain folders all at the same level.
The folder structure is parallel so you can't possibly access subdomainA with FolderA in the URL.
From the error and the descriptions above, the URLs are:
example.com/primaryA/
example.com/cgi-bin/
example.com/folderA/
example.com/subdomainA/
example.com/subdomainB/

The error:
Not Found
The requested URL /folderA/subdomA.com/ was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
makes perfect sense because both folders are in the root directory. The subdomainA folder would need to be in folderA to use that URL without the error.

The contents (not the folder itself) for the Primary domain needs to be in the root directory with folderA right where it is and the subdomains A&B folders in folderA is my best guess, not having seen the instructions for installation of the script.

lucy24




msg:4688070
 3:28 am on Jul 16, 2014 (gmt 0)

Here's the ambiguity:
The primary domain is in the folder public_html. public_html contains four folders, one for subdomainA, one for subdomainB, a folder called cgi-bin and folderA which contains the index.php file for the primary domain.

I read that as: DNS and/or server alias* for example.com points to "folderA". So if you request example.com you are sent to folderA, at which point the server looks for-- and finds-- the index.php file.

If, instead, the DNS points only to /public_html/ then index.php is simply in the wrong place. So you have to either move the file or change the DNS.

raydona, this isn't your own server is it? I got the impression it wasn't.

Since subdomains by their nature always involve aliasing, either
/user/subdomain/
or
/user/sitename/subdomain/
or, for that matter,
/different-server-in-a-different-part-of-the-country/subdomain/
could be made to work. But you have to pick one. And then, if the physical configuration is
/user/sitename/subdomain/
you have to add a rule to make sure nobody requests
example.com/subdomain/etcetera.

Is this an existing site or a brand-new one? If it's new, you can take time to decide which physical configuration will work best in the long run. If it's an existing site that stopped working, you will probably have to put things back the way they were.


* Entirely different things, of course, but here all that matters is "stuff you can do yourself in htaccess" vs. "stuff you need to bug the host about". I'll say "DNS" to save time.

raydona




msg:4692770
 12:49 am on Aug 4, 2014 (gmt 0)

Hello, Sorry for the late reply but I'm in involved in a web project which means I can't devote the time I would like to resolving this problem. I used to have an index.html file in my primary domain and there was no problem in accessing my primary domain, subdomains A and B. The problem started when I placed the ecommerce script in the primary domain, and as previously stated the ecommerce site required me to place the contents in a separate folder for security reasons.
As previously stated, I do have a .htaccess file in the primary domain which clearly isn't working. Would it be possible to create a .htaccess file to do something like this:
if(address of primary domain) go to folderA;
else if (address of subdomainA) go to folder containing subdomainA;
else if (address of subdomainB) go to folder containing subdomainB;

I have tried pulling the index.php file out of folderA and placing it in the folder containing the primary domain, but that makes the whole site inoperable because the directory structure is broken.

lucy24




msg:4692774
 1:16 am on Aug 4, 2014 (gmt 0)

Would it be possible to create a .htaccess file to do something like this

It sounds as if you're talking about server aliasing, which can only be done in the config file. You have to ask your host to do it for you. (Isn't this already happening, though?) Requests for subdomains will never even reach your corner of the server unless the config file knows where to send them.

One further thing to watch out for: If the directories for the subdomains are physically located inside the directory that holds the primary domain, then all subdomain requests must pass through the primary htaccess. If, instead, all directories are parallel, then you can give each area its own htaccess without worrying about its effect on requests for other hostnames.

not2easy




msg:4692787
 3:59 am on Aug 4, 2014 (gmt 0)

It would be so much simpler to just create an index.html (or index.php) file and put that in the domain root with links to the script folder: <a href="/folderA/">. That would go to the index.php in folderA and no rewrite is needed. The subdomains A & B can link from the root or from folderA, wherever it is logical. It is not an uncommon structure to have a "front page" that introduces your brand, products, purpose, policies and how to navigate the parts of your site. Just my view of this layout.

raydona




msg:4703031
 1:22 am on Sep 21, 2014 (gmt 0)

Sorry, I've been really busy so didn't have time to log in to thank lucy24 and not2easy for all their kind help. I've adopted "create an index.html (or index.php) file and put that in the domain root with links to the script folder: <a href="/folderA/">". That has worked for me, solved my problem. So again many thanks for all your help.

not2easy




msg:4703035
 3:36 am on Sep 21, 2014 (gmt 0)

Good to hear you had success.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved