Redirect domain to subfolder

Last night I was looking for a way to redirect a domain (example.com) to example.com/folder but without actually redirecting the user to that folder.

So I thought that the best way to go about is actually treading every request as example.com/folder.

Anyway enough said here is the result.

[code lang="php"] /* * Redirect to a different folder of the current domain that was requested */ $host = $SERVER['HTTPHOST']; $extra = ‘~ckr’; header(”Location: http://$host/$extra”); exit; [/code]

Pretty need isn’t it? It only took 4 lines.


11 Responses to “Redirect domain to subfolder”

  • Marios Tziortzis Says:

    Nice, but you can also use .htaccess to do that. I think it’s faster

  • Constantinos Says:

    Yes you are right, .htaccess is faster but doesn’t do what I want it to do. You see I want the visitor to go to example.com/~ckr but only type and see example.com.

    So in example.com would only have one file (index.php) but if i request example.com/contact.php it will load example.com/~ckr/contact.php without showing it to the visitor.

    Hope this clears things out.

  • Marios Tziortzis Says:

    No, i did understand correctly :P

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /sample/$1 [L]

    This should do the trick

    Now if you need something more ‘web2.0ish’ you could use

    RewriteRule ^(.*)$ /parser.php?args=$1 [L]

    and let your ‘parser.php’ to slice and dice you variables. So you could have something like
    domain.com/page/contact or even domain.com/user/remedix
    I think you got the point..

    or even to simplify it

    RewriteRule . /index.php [L]

    The above is the Zend Framework’s way..along with some sweet php classes that do the rest..i am using a slightly modify version of that on some of my systems..Works perfectly.

    Hope this helps.

  • Constantinos Says:

    I do agree with you that .htaccess and RewriteRules is the faster solution to go. But since my RewriteRules knowledge is somewhat limited, the first thing that came to mind was that php script.

    I do use the some of the above myself but only if I am passing parameters into a php file and I want to have a more web2.0ish URLs. Something like wordpress.

    Thanks for the tip though I will definitely give it a try.

  • lucas13 Says:

    What if you need this:

    you have domain http://www.domain1.com and http://www.domain2.com. Domain1 is pointing (name servers) to server where domain2 is hosted. So at the moment if you type down domain2.com OR domain1.com it will always show you the same thing. But I created subfolder on the hosting and I want domain1 to be chowing content of that subfolder (but also keep domain2.com to be chowing the content of the root) ?

    Thanks
    lucas13

  • jack Says:

    im trying to figure out your take on whats better for moving
    http://www.old-domain.com to http://www.new-domain.com
    htaccess or php 301 redirect?

  • Constantinos Says:

    @jack
    [quote comment="16138"]im trying to figure out your take on whats better for moving
    http://www.old-domain.com to http://www.new-domain.com
    htaccess or php 301 redirect?[/quote]

    I would go with htaccess.

  • David Says:

    [quote comment="10762"]What if you need this:

    you have domain http://www.domain1.com and http://www.domain2.com. Domain1 is pointing (name servers) to server where domain2 is hosted. So at the moment if you type down domain2.com OR domain1.com it will always show you the same thing. But I created subfolder on the hosting and I want domain1 to be chowing content of that subfolder (but also keep domain2.com to be chowing the content of the root) ?

    Thanks
    lucas13[/quote]

    HOW DO YOU DO THAT?!

    I have phyar.net and roleplaychat.info both on the same host.

    I want phyar.net/rpc to be roleplaychat.info without it having to be roleplaychat.info/rpc

    Please make a full code with either PHP or .htaccess for me that does this omg

  • Busby SEO Challenge Says:

    I still cannot redirect my root domain to my sub folder

  • Constantinos Kouloumbris Says:

    [quote comment="41526"]Is it possible to redirect http://www.mydomain.com/index.html to http://www.mydomain.com ??[/quote]

    Hi,

    I am sorry but you have me a little confused, is both domains with the same name? because if they are then you need some kind of other configuration that what this article was about.

Leave a Reply