SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    .htaccess rewrite not working if subdirectory also contains .htaccess file.

    Although I have been developing web pages for a few years (HTML, PHP, MYSQL), I am a novice using mod_rewrite. I have read several tutorials and many seem contradictory. I may be going about this all wrong so I am seeking guidance from someone with experience in this area.

    I am having trouble with a redirect in my top level domain if a subdirectory also contains a .htaccess file.

    To help explain, here is my directory structure.

    "www.domain1.com" points to directory /public_html

    "www.domain2.com" points to directory /public_html/domain2

    My goal is to keep people from directly accessing directory domain2 using "http://www.domain1.com/domain2/" I want them to access it only by "http://www.domain2.com/"

    Here is the .htaccess file that I have placed in directory /public_html

    Code:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^domain1\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.domain1\.com$
    RewriteRule ^domain2\/?(.*)$  - [F]
    If I do not have a .htaccess file in directory /public_html/domain2, then this works fine and properly displays the Forbidden error.

    In directory /public_html/domain2 I also have the following .htaccess file:

    Code:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^domain2\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.domain2\.com$
    RewriteRule ^forums\/?(.*)$ http://forums.domain2.com/$1 [R=301,L]
    I want users to be sent to "http://forums.domain2.com" when they enter "http://www.domain2.com/forums/" (this redirection works fine)

    The problem I am encountering is when directory /public_html/domain2 contains a .htaccess file, then the rewrite rule in the .htaccess file in directory /public_html for domain "www.domain1.com" does not work (the page on the /domain2 directory is displayed when the desired result is to display the forbidden error).

    I am running Apache 2.2.14.

    Any assistance will be appreciated.

    Steve

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,644
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    SS51,

    The root of your problem is in understanding .htaccess and domains.

    An .htaccess file is read (and parsed) ONLY if it is in the path to the requested file, thus, domain1's .htaccess will NOT be read if domain2 is requested directly; it will be read if the request is domain1/domain2.

    As for your forum.domain2, I don't believe I've ever seen that setup on a host although it should be possible. May I assume that it's DocumentRoot is domain1/domain2/forum?

    Excellent: A statement of your goal!
    My goal is to keep people from directly accessing directory domain2 using "http://www.domain1.com/domain2/" I want them to access it only by "http://www.domain2.com/"
    However, your code,
    Code:
    # .htaccess file that I have placed in directory /public_html
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^domain1\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.domain1\.com$
    RewriteRule ^domain2\/?(.*)$  - [F]
    ... is designed to KILL any request made via domain1, not redirect it via domain2 which
    Code:
    RewriteEngine on
    RewriteRule ^domain2/(.*)$ http://domain2/$1 [R=301,L]
    ... would do.

    Your stated goal is now met.

    However, dealing with forum.domain2 may also require some treatment in domain2's .htaccess. Please note that some hosts will make this impossible as they'll parse the subdomain out and redirect to the subdirectory - ATTACK your host if they do this to you!

    To ensure that the forum subdomain is used rather than the domain2/forum,
    Code:
    # .htaccess in domain2
    RewriteEngine on
    RewriteRule ^forum/(.*)$ http://forum.domain2/$1 [R=301,L]
    # This assumes that you have a good/cooperative host
    Finally, two comments on optimizing your coding
    Code:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^domain2\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.domain2\.com$
    RewriteRule ^forums\/?(.*)$ http://forums.domain2.com/$1 [R=301,L]
    1. Because I've already shown code (above) which does not use either of the RewriteCond statements, you should know that they're not necessary (i.e., should be removed) and

    2. If you did need the two RewriteCond's, combine them by making the www. optional, i.e.,
    Code:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$
    RewriteRule ^forums\/?(.*)$ http://forums.domain2.com/$1 [R=301,L]
    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    dklynn,

    Thank you very much for your detailed reply. I actually have two goals but I did not do a very good job of explaining them. I would be glad to provide real domain names pointing to real web pages, but I am unsure if that is acceptable on this board. I apologize if I attempt to provide too many details.

    Some information:

    If a user navigates to "http://www.domain1.com/" the files in folder "/home/myaccount/public_html/" will be viewed (This is the top level folder for html. All other domains are sub-folders of this directory).

    If a user navigates to "http://www.domain2.com/" the files in folder "/home/myaccount/public_html/domain2/" will be viewed.

    If a user navigates to "http://www.domain1.com/domain2/" the files in folder "/home/myaccount/public_html/domain2/" will also be viewed.

    If a user navigates to "http://forums.domain2.com/" the files in folder "/home/myaccount/public_html/domain2/forums/" will be viewed.

    If a user navigates to "http://www.domain2.com/forums/" the files in folder "/home/myaccount/public_html/domain2/forums/" will also be viewed.



    ***********************************************************
    Goal 1:

    If a users navigates to "http://www.domain2.com/forums/", I want them redirected to "http://forums.domain2.com/". In folder "/home/myaccount/public_html/domain2/" I have placed the following .htaccess file and it works as desired (although at your suggestion, I will remove the unnecessary lines).

    Code:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^domain2\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.domain2\.com$
    RewriteRule ^forums\/?(.*)$ http://forums.domain2.com/$1 [R=301,L]
    ***********************************************************
    Goal 2:

    If a user navigates to "http://www.domain1.com/domain2/" I want them to receive the forbidden error. The good guys (members of the club) know the proper URL. The bad guys use "http://www.domain1.com/domain2/". The only acceptable way to get to directory "/home/myaccount/public_html/domain2/" is to navigate to "http://www.domain2.com/". I want this so I can keep my statistics accurate. If they navigate to "http://www.domain1.com/domain2/" then this access is listed in the domain1 log instead of the domain2 log. In folder "/home/myaccount/public_html/" I have placed the following .htaccess file

    Code:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^domain1\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.domain1\.com$
    RewriteRule ^domain2\/?(.*)$  - [F]
    This part works properly if the .htaccess file associated with Goal 1 is not present in folder "/home/myaccount/public_html/domain2/". As soon as the Goal 1 .htaccess file is added, the user no longer receives the forbidden error.
    ***********************************************************

    There must be some interaction between the two .htaccess files that is causing the problem or else I am violating some apache rule.

    Again - Thanks for the assistance.

    Steve

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,644
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    SS51,

    No problem.

    The real domain names are not necessary.
    Quote Originally Posted by SS51
    Some information:

    If a user navigates to "http://www.domain1.com/" the files in folder "/home/myaccount/public_html/" will be viewed (This is the top level folder for html. All other domains are sub-folders of this directory).
    SOP with my hosts, too.
    If a user navigates to "http://www.domain2.com/" the files in folder "/home/myaccount/public_html/domain2/" will be viewed.
    Ditto - and this will bypass domain1's .htaccess file.
    If a user navigates to "http://www.domain1.com/domain2/" the files in folder "/home/myaccount/public_html/domain2/" will also be viewed.
    Ditto - except that .htaccess from domain1 will be processed THEN the .htaccess in domain2. This is the only way that both .htaccess files will be read.
    If a user navigates to "http://forums.domain2.com/" the files in folder "/home/myaccount/public_html/domain2/forums/" will be viewed.
    Thank you, I think I had assumed this was the case (or wondered about a subdomain on a subdomain [addon domain]).
    If a user navigates to "http://www.domain2.com/forums/" the files in folder "/home/myaccount/public_html/domain2/forums/" will also be viewed.
    As should be the case.

    ***********************************************************
    Goal 1:

    If a users navigates to "http://www.domain2.com/forums/", I want them redirected to "http://forums.domain2.com/". In folder "/home/myaccount/public_html/domain2/" I have placed the following .htaccess file and it works as desired (although at your suggestion, I will remove the unnecessary lines).
    You've got it!
    Code:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^domain2\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.domain2\.com$
    RewriteRule ^forums\/?(.*)$ http://forums.domain2.com/$1 [R=301,L]
    # no need to escape /'s in the regex
    ***********************************************************
    Goal 2:

    If a user navigates to "http://www.domain1.com/domain2/" I want them to receive the forbidden error. The good guys (members of the club) know the proper URL. The bad guys use "http://www.domain1.com/domain2/". The only acceptable way to get to directory "/home/myaccount/public_html/domain2/" is to navigate to "http://www.domain2.com/". I want this so I can keep my statistics accurate. If they navigate to "http://www.domain1.com/domain2/" then this access is listed in the domain1 log instead of the domain2 log. In folder "/home/myaccount/public_html/" I have placed the following .htaccess file
    Ah! The missing "goal" from above. In that case, you've gotten this correct, too (except for "optimizing" the RewriteCond statements and escaping the /). {Duh - see below.}
    Code:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^domain1\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.domain1\.com$
    RewriteRule ^domain2\/?(.*)$  - [F]
    This part works properly if the .htaccess file associated with Goal 1 is not present in folder "/home/myaccount/public_html/domain2/". As soon as the Goal 1 .htaccess file is added, the user no longer receives the forbidden error.
    Okay, I see that the (escaped) / is optional AND that you're allowing more in the {REQUEST_URI}. Why? If all you're doing with this RewriteRule, you have no need of anything beyond domain2/ in the regex. If, for some weird reason (server configuration?) this does not merely return a 400 (bad request), 401 (unauthorized), 403 (forbidden), 406 (not acceptable) ..., replace the - [F] with an absolute redirection to another domain - or your own DocumentRoot (domain1).

    ***********************************************************

    There must be some interaction between the two .htaccess files that is causing the problem or else I am violating some apache rule.
    .htaccess files do not interact. The logic used in one file may impact the logic of the other but only if the redirection gets to the other's directory. .htaccess is a file structure configuration file and can only be read if its directory is in the path of the request and only in order (from DocumentRoot to the requested file's directory.
    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    dklynn,

    Again - Thanks for your reply. At this point, I'm ready to give up on this. For goal #2 (from domain 1) I attempted to forward to another site instead of using F (forbidden) and it still will not work if there is anything in the .htaccess file for domain2. In domain2 I changed the .htaccess file to just

    Code:
    RewriteEngine on
    (literally one line in the file and no Rewrite Rules) and it (goal #2) still did not work. When I changed the .htaccess file in domain2 to

    Code:
    ##RewriteEngine on
    (everything commented out) then goal #2 worked properly and redirected to another site.

    I don't expect any more assistance on this, but if anyone gets really bored some afternoon, they may want to simulate this scenario just to see what happens.

    All of my testing has been on my local computer running Apache. I never send anything to the live server until it is proven out on my local server. Possibly there is some setting on my local server that is causing this.

    Hopefully, in the future I'll be able to answer some questions on this forum instead of just asking them.

    Steve

  6. #6
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,644
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    SS51,

    If I have to guess, your host is trying to do you a favor (with Apache's settings - nothing personal) by attempting to connect to the subdomain (probably via mod_alias) before allowing mod_rewrite to do its work. If this is the case, I'd modify domain2's .htaccess to include a look at Apache's {THE_REQUEST} variable (which includes HTTP Protocol, the ORIGINAL {REQUEST_URI} and the GET/POST) and, if domain2 is in %{THE_REQUEST}, fail it right then and there with ... aw:
    Code:
    # .htaccess in domain2 addition (right after RewriteEngine on)
    RewriteCond %{THE_REQUEST} domain2
    RewriteRule .? - [F]
    There may be a better way to do it but the power of mod_rewrite can shine through whatever nonsense is going on with your server.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    SS51,

    If I have to guess, your host is trying to do you a favor (with Apache's settings - nothing personal) by attempting to connect to the subdomain (probably via mod_alias) before allowing mod_rewrite to do its work. If this is the case, I'd modify domain2's .htaccess to include a look at Apache's {THE_REQUEST} variable (which includes HTTP Protocol, the ORIGINAL {REQUEST_URI} and the GET/POST) and, if domain2 is in %{THE_REQUEST}, fail it right then and there with ... aw:
    Code:
    # .htaccess in domain2 addition (right after RewriteEngine on)
    RewriteCond %{THE_REQUEST} domain2
    RewriteRule .? - [F]
    There may be a better way to do it but the power of mod_rewrite can shine through whatever nonsense is going on with your server.

    Regards,

    DK
    Success - That worked great on my local server. Now to test it on the production server.

    Looking on the bright side of these difficulties, I now have a much better understanding of mod_rewrite.

    Thanks again,
    Steve

  8. #8
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,644
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)


    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  9. #9
    SitePoint Member
    Join Date
    Mar 2007
    Location
    Fort Lauderdale, FL
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Late Entry ...

    Code:
    Options -Indexes +FollowSymLinks
    
    <IfModule mod_rewrite.c>
    
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www\.domain1\.com 
    RewriteCond %{REQUEST_URI} ^/domain2 [NC]
    RewriteRule . / [R=301,L]
    
    </IfModule>

  10. #10
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,644
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Edward,

    OMG, NO!

    Code:
    # Those are core directives where -Indexes prohibits directory listings and
    # +FollowSymLinks helps to enable mod_rewrite (and is something which should be in the httpd.conf, not in .htaccess
    
    <IfModule mod_rewrite.c>
    # A MAJOR abuse of the server! So bad, in fact, that I had to create a standard rant for it:
    # [rant #4]
    The definition of an idiot is someone who repeatedly does the same thing expecting a different result. Asking Apache to confirm the existence of ANY module with an <IfModule> ... </IfModule> wrapper is the same thing in the webmaster world. DON'T BE AN IDIOT! If you don't know whether a module is enabled, run the test ONCE then REMOVE the wrapper as it is EXTREMELY wasteful of Apache's resources (and should NEVER be allowed on a shared server).
    [/rant 4] RewriteEngine On RewriteBase / # Another no-no as this is designed to undo a mod_alias redirection before handoff to mod_rewrite # Where is your Redirect statement? RewriteCond %{HTTP_HOST} ^www\.domain1\.com [NC] # No Case flag is required else DoMaIn.CoM will not be matched (and it IS legal; the host is not case sensitive) RewriteCond %{REQUEST_URI} ^/domain2 [NC] # leading / is only matched by Apache 1.x servers # {REQUEST_URI} variables are case sensitive so you got these backward RewriteRule . / [R=301,L] # redirect EVERYTHING to the DirectoryIndex of the root? </IfModule>
    Sorry to "go off" on you like that but I do get pedantic when I see errors in code. It's not personal (I don't know you), it's to help you as well as prevent other members from making the same errors (or using your code).

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •