Forum Moderators: phranque

Message Too Old, No Replies

Need help with subdomain htaccess

         

dev_ch7

12:55 pm on Oct 31, 2011 (gmt 0)

10+ Year Member



Hello everyone,

I have a root domain and sub domain. Both of them have separate .htaccess file.


The sub-domain (http://directory.mysite.com) is pointing to a folder ( folder-name: directory) situated in the root directory.



.htaccess file of root domain (http://www.mysite.com) is as follows:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^directory\.mysite\.com
RewriteRule ^(.*)$ /directory/$1 [L]


RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^login$ user_login.php




.htaccess file of sub-domain (http://directory.mysite.com) is as follows

RewriteEngine on

RewriteRule ^login$ login.php



The htaccess of root is working fine. But the htaccess of sub-domain is not working. The index page (welcome page) of sub domain is opening, but its not going to other pages like login, register etc. instead of that its generating Error 404.



Can anybody help me with this. I am a beginner with htaccess. If possible, let me know the details so that every beginner can set up the htaccess without any problem/ difficulty.

lucy24

4:10 pm on Oct 31, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Can you be a little more specific? I'm going to guess that you have site-absolute links like /somedirectory/somepage.html that aren't working in the subdomain. But in htaccess, you do not want to base a diagnosis on a lucky guess ;)

Look at your browser's address bar when you get a 404. What does it say? A 404 is a special kind of rewrite, so it will always have the name of the page your browser thinks you're going to, not the actual address of the 404 page.

dev_ch7

7:39 pm on Oct 31, 2011 (gmt 0)

10+ Year Member



hello,

Yes, actually i am going to the login.php using "login" keyword from the url and this i am doing using the following code.

RewriteRule ^login$ login.php


As i mentioned earlier, this same process will be applicable for the sub-domain as well.

Forgot to mention on my earlier topic about the Errordocument.


.htaccess file of root domain (http://www.mysite.com) is as follows:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^directory\.mysite\.com
RewriteRule ^(.*)$ /directory/$1 [L]


RewriteCond %{REQUEST_FILENAME} !-f

ErrorDocument 404 /404.php

RewriteRule ^login$ login.php
RewriteRule ^signup$ registration.php
RewriteRule ^forgot_my_password$ forgot_password.php



.htaccess file of sub-domain (http://directory.mysite.com) is as follows. Few file names are different for sub-domain.

RewriteEngine on

ErrorDocument 404 /404.php

RewriteRule ^login$ user_login.php
RewriteRule ^signup$ user_registration.php
RewriteRule ^forgot_my_password$ forgot_password.php


Please help me on this. What mistake i am doing here? What should i do?

Thanks in advance.

g1smd

7:44 pm on Oct 31, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Add the [L] flag to every rule.

Omitting it can cause all sorts of issues.

zabalex

7:49 pm on Oct 31, 2011 (gmt 0)

10+ Year Member



Hi,
please make sure you have configured the sub domain in httpd.conf present in the root directory to point the subdomain home directory. If your entry in httpd.conf is configured properly you can set the login path in .htaccess as described in main domain.

regards
zabalex

dev_ch7

7:50 pm on Oct 31, 2011 (gmt 0)

10+ Year Member



hello g1smd,

thanks for your quick reply.

Do you mean to say i should modify the code like this:

RewriteRule ^login$ user_login.php [L]

If so, then in which .htaccess file i should use? The .htaccess of root domain or in the .htaccess of sub-domain ? Or on both .htaccess? I have two separate .htaccess for root and subdomain on their respective directory.

dev_ch7

7:55 pm on Oct 31, 2011 (gmt 0)

10+ Year Member



Hello zabalex,

My site is hosted on godaddy. I kept all my sub-domain files on a root folder (named: news). I made a sub-domain (http://news.mysite.com) using go-daddy's cpanel and pointed the folder (news) for this sub-domain.


The login path for root domain and for sub-domain is different. Also the file name is different.

For the root domain it is login.php and for the sub-domain it is user_login.php.

login.php is on root directory and user_login.php is under "news" folder (news folder is also on root directory and it is also the sub-domain name).

lucy24

9:50 pm on Oct 31, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



then in which .htaccess file i should use? The .htaccess of root domain or in the .htaccess of sub-domain ? Or on both .htaccess? I have two separate .htaccess for root and subdomain on their respective directory.

This is why I asked the original question. Never mind the names; look only at the physical directory.

Are the two domains ("real" domain and "subdomain") located in parallel directories? That is, side by side, not one inside the other? If so, each htaccess will only see the requests for its own (sub)domain. Anything concerning www.example.com will never be seen by subdomain.example.com, and vice versa.

Or is the subdomain in a directory inside the main domain's directory? That is, in the same location where you'd keep www.example.com/stuff and www.example.com/morestuff and so on? If so, anything that involves both (sub)domains needs to go in the first htaccess.

Does anything in your htaccess involve switching between one (sub)domain and the other?

dev_ch7

5:11 am on Nov 1, 2011 (gmt 0)

10+ Year Member



Hello lucy24,

All my folders and files (for eg. folders: uploads, plugins, lib and files: index.php, header.php, footer.php) of root domain (http://www.mysite.com) are in the root directory.

The subdomain folder named "news" is also in the root directory with other folders like uploads, plugins. Inside news folder i have separate files and folders for subdomain (http://news.mysite.com).

.htaccess of root domain is also in the root directory with header.php, footer.php

.htaccess of subdomain is under news folder.

lucy24

7:15 am on Nov 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I suspect it's misinterpreting site-absolute links as referring to the main directory, not the subdirectory, so everything is off by one level.

Try this. Put a RewriteRule in your subdomain's htaccess that says something like

RewriteRule garbage\.html {some path that's supposed to work} [R=301,L]

For the first part, make up a nonsense name-- something that doesn't really exist. For the second part, pick a file and path that do exist, but don't write out the full http://subdomain.example.com. Instead just use a leading slash.

Do you end up on your 404 page? Look at your browser's address bar and see what it says.

Can you get to the subdomain's index page on purpose-- that is, by typing its full address instead of being rewritten from something else? If so, do the links behave any differently if you got there one way or the other? Same thing for any interior pages.

dev_ch7

7:34 am on Nov 1, 2011 (gmt 0)

10+ Year Member





Can you get to the subdomain's index page on purpose-- that is, by typing its full address instead of being rewritten from something else? If so, do the links behave any differently if you got there one way or the other? Same thing for any interior pages.




Sorry, i forgot to mention this earlier. From the beginning itself, if i type http:\\subdomain.example.com it was opening the index.php file of news folder(subdomain folder name) which is different from the root domain's (example.com) index.php page. But, the other pages of subdomain like http:\\news.example.com\login or http:\\news.example.com\terms is not opening. instead of that, it was displaying 404.php page which is again different from the root domain's 404.php page (By design and text message).



Try this. Put a RewriteRule in your subdomain's htaccess that says something like

RewriteRule garbage\.html {some path that's supposed to work} [R=301,L]

For the first part, make up a nonsense name-- something that doesn't really exist. For the second part, pick a file and path that do exist, but don't write out the full [subdomain.example.com....] Instead just use a leading slash.

Do you end up on your 404 page? Look at your browser's address bar and see what it says.



I wrote the following code on .htaccess of news folder.

RewriteEngine on

RewriteRule abc\.html /add_video.php [R=301,L]


add_video.php is an existing file located on news folder.


I tried to access http:\\news.example.com\abc.html , it was displaying 404 page. Also the url changed into http:\\news.example.com\login

lucy24

10:51 pm on Nov 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



it was displaying 404.php page which is again different from the root domain's 404.php page (By design and text message).

Oh, that's a terrific idea. It means that even if you're rewriting rather than redirecting, you'll know which (sub)domain your brower "thinks" you are in. Way ahead of me :)

I tried to access http:\\news.example.com\abc.html , it was displaying 404 page. Also the url changed into http:\\news.example.com\login

The explanation is now on the horizon. There's an additional rewrite happening that isn't supposed to be happening. (We'll assume for the sake of discussion that the backslashes in your last paragraph are typos for regular front-slashes ;))

Based on your first post, requests for extensionless "/login" will always be rewritten-- but not redirected-- right? Either to user_login.php if in the main domain, or to login.php if in the subdomain.

So the browser is trying to fetch content from the nonexistent subdomain.com/user_login.php, not finding it, and instead displaying the 404.

Time off here for cogitation. Someone will sort it out, but I'm getting dizzy.

dev_ch7

5:12 am on Nov 2, 2011 (gmt 0)

10+ Year Member




(We'll assume for the sake of discussion that the backslashes in your last paragraph are typos for regular front-slashes ;))


Yes, thats true.

Let me be clear one more point here.

Currently i have the following codes written on my root domain's [http:\\www.example.com] .htaccess.


RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

ErrorDocument 404 /404.php

RewriteRule ^signup$ registration.php
RewriteRule ^login$ index.php
RewriteRule ^home$ index.php




And, I have an empty .htaccess file on subdomain [http:\\news.example.com] folder (folder name : "news")



In this scenario: if i type http:\\news.example.com on browser's address bar its opening the index.php. But if i try to visit other pages like login or anything, without opening the respective pages its opening sub-domain's custom 404.php page (sub-domain's 404.php is different from the root domain's 404.php page). But, if i type http:\\news.example.com\registration.php, then its displaying the registration page of sub-domain.

Few page names are same for root(main) and sub domain like the registration page. On both the folder(root directory and news folder), i have registration.php page and i am trying to get this page using http:\\www.example.com\signup (getting this page) and http:\\news.example.com\signup (displaying 404.php page) .

lucy24

7:16 am on Nov 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^login$ user_login.php


Let's digress a little.

The !-f condition is hardly ever necessary; it just makes extra work for the server. Here it's really, really unnecessary because your rule involves specific filenames. Either "login" exists, or it doesn't. (Technically it doesn't. This will always be true, because extensionless files don't exist, even if you've dressed them up to make users think they do.)

Same goes for !-d

Write your rules so they will do what they're supposed to do. If requests involve nonexistent files or directories, that's what the 404 page is for.

Put the [L] flag at the end of every RewriteRule unless you have a clear and specific reason for leaving it out. In practice that means Skips or Chains, and we are not going to talk about those.

Oh, wait. When you said the subdomain's htaccess is empty, did you mean that literally? Then how does it know to use the subdomain's 404 page instead of the regular one? Is it handled with some kind of <directory> instruction in the main htaccess? If you request "primarydomain.com/somegarbage" does it give you the primary 404 page?

Matter of fact, there's an ongoing thread elsewhere in this forum where the poster's new domain flatly refuses to use the custom 404. Maybe the two of you could get together; you each seem to have half of a problem and half of a solution :)

g1smd

7:32 am on Nov 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



From the code above, you have no RewriteRule associated with your three RewriteConds.

The three RewriteRules also have no conditions attatched to them.

Conditions only ever apply to the single following RewriteRule. So if the same conditions apply to more than one rule, the conditions must be restated before each rule.

dev_ch7

7:41 am on Nov 2, 2011 (gmt 0)

10+ Year Member



Is it handled with some kind of <directory> instruction in the main htaccess?


There is no condition or code written on htaccess except the above codes of my previous post.


If you request "primarydomain.com/somegarbage" does it give you the primary 404 page?


Yes, its displaying the primary (main) domain's 404 page.


Matter of fact, there's an ongoing thread elsewhere in this forum where the poster's new domain flatly refuses to use the custom 404.


Can you post the link here?

Thanks in advance.

dev_ch7

7:48 am on Nov 2, 2011 (gmt 0)

10+ Year Member



From the code above, you have no RewriteRule associated with your three RewriteConds.

The three RewriteRules also have no conditions attatched to them.

Conditions only ever apply to the single following RewriteRule. So if the same conditions apply to more than one rule, the conditions must be restated before each rule.


Can you give some clue about the conditions? I am not understanding what may be the condition and how to apply that condition for main domain and subdomain separately? Do i need to use the condition on the same (main domain's) htaccess?

g1smd

8:12 am on Nov 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The correct syntax is NOT:

RewriteCond <whatever>
RewriteCond <whatever>
RewriteCond <whatever>

some other code

RewriteRule <whatever>
RewriteRule <whatever>
RewriteRule <whatever>



The correct syntax IS:

RewriteCond <whatever>
RewriteCond <whatever>
RewriteCond <whatever>
RewriteRule <whatever>

RewriteCond <whatever>
RewriteCond <whatever>
RewriteCond <whatever>
RewriteRule <whatever>

RewriteCond <whatever>
RewriteCond <whatever>
RewriteCond <whatever>
RewriteRule <whatever>