Results 1 to 7 of 7

Thread: How to make a subfolder the default website, without showing subfolder in URL?

  1. #1
    Join Date
    Nov 2011
    Posts
    3

    Default How to make a subfolder the default website, without showing subfolder in URL?

    Hi,

    I have two subfolders unders /public_html:

    /web - a basic html site
    /wp - a Wordpress site, currently under development

    When I browse to http://www.mydomain.com, the Wordpress site is returned. That'll be cool when the Wordpress site is completed. However, in the mean time I want the "html" site returned.

    What do I need to change so that the html site is returned, without the subfolder showing up in the URL? For example, I want the the site under /public_html/web returned, but I want the URL to still display "http://www.mydomain.com".

    I tried messing with the root index.php and .htaccess, but no luck... I'm just not familiar enough with how this works.

    Thanks.

  2. #2
    Join Date
    Feb 2006
    Location
    Florida, USA
    Posts
    1,498

    Default

    In a .htaccess file under public_html put
    Code:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$
    RewriteCond %{REQUEST_URI} !^/web/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /web/$1 [L]
    RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$
    RewriteRule ^$ /web/ [L]
    Replace the 3 instances of "web" with your subfolder name and "mydomain" with whatever domain you're using.
    If at first you don't succeed, try reading the instructions.
    semlar.com

  3. #3
    Join Date
    Nov 2011
    Posts
    3

    Default

    Thanks a ton! I'm still using the BlueHost temporary URL (boxABC.bluehost.com/~mydomain) so it's not working yet, but I'm sure it will when I change my DNS.

    One quick followup question: in /public_html/index.php, should I have simply a redirect to "www.mydomain.com/web"? (It currently only has the "require('./wp/wp-blog-header.php')" statement for wordpress).

  4. #4
    Join Date
    Feb 2006
    Location
    Fresno, CA
    Posts
    629

    Default

    I think basil was trying to tell you that if you add the above code to .htaccess in the public_html folder, it will redirect traffic to the web folder

  5. #5
    Join Date
    Feb 2006
    Location
    Florida, USA
    Posts
    1,498

    Default

    It won't matter what you have in your index file, unless the file is being called directly (e.g. example.com/index.php instead of example.com). The last line matches an empty path to cover all bases.

    I was in kind of a hurry earlier, but I'll break it down for you.

    RewriteCond defines conditions for the RewriteRule:
    %{HTTP_HOST} matches the domain, we're restricting the rule to a domain matching either "mydomain.com" or "www.mydomain.com" only.
    %{REQUEST_URI} matches the path, we're excluding anything starting with /web/ so we don't cause an infinite loop when it redirects then checks the htaccess file again to see what to do, otherwise it'll just keep adding /web/ to the new path forever.
    %{REQUEST_FILENAME} these two lines are basically checking that the request doesn't match an existing file or directory, if it exists the condition will fail. This lets you still access example.com/wp without it redirecting to /web/wp.
    RewriteRule ^(.*)$ /web/$1 finally, if all the conditions are met, we take the requested path (everything after the domain name) and silently redirect it to /web/etc.
    The last two lines are separate from the rest, and its sole purpose is to match an empty request, which would normally resolve to whatever your index file is, and redirect it. This is necessary because the index file probably exists, and if it exists it won't be covered by the earlier condition which ignores existing files.

    Here's a thing and another thing for some light reading.

    For the temporary url you could probably get away with something like this..
    Code:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^(www\.)?box\d+\.bluehost\.com$
    RewriteCond %{REQUEST_URI} !^/~replaceme/web/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^~replaceme/(.*)$ /~replaceme/web/$1 [L]
    RewriteCond %{HTTP_HOST} ^(www\.)?box\d+\.bluehost\.com$
    RewriteRule ^~replaceme/?$ /~replaceme/web/ [L]
    Last edited by Basil; 11-18-2011 at 06:36 PM.
    If at first you don't succeed, try reading the instructions.
    semlar.com

  6. #6
    Join Date
    Nov 2011
    Posts
    3

    Default

    I really appreciate the explanation and additional code. I will give it a try, and I'll also check out the mod_rewrite docs. I use regex so I understand that part of, it's those RewriteCond and RewriteRule statements that are throwing me!

    Thanks again.

  7. #7
    Join Date
    Feb 2006
    Location
    Florida, USA
    Posts
    1,498

    Default

    Ah, good! If you already know regex the rest is mostly syntax. You shouldn't have too much trouble with it.
    If at first you don't succeed, try reading the instructions.
    semlar.com

Posting Permissions

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