Clippings

  • Redirect domain to subfolder

    Posted Oct 2nd, 2006 in Clippings With | 11 Comments

    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.

    /*
     * Redirect to a different folder of the current domain that was requested
     */
    $host = $_SERVER['HTTP_HOST'];
    $extra = '~ckr';
    header("Location: http://$host/$extra");
    exit;
    

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


Comments (11)

Marios Tziortzis » 16. Oct, 2006

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

Constantinos » 16. Oct, 2006

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 » 18. Oct, 2006

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 » 20. Oct, 2006

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 » 17. Nov, 2006

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 » 23. Mar, 2007

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 » 23. Mar, 2007

@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 » 28. May, 2008

[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 » 13. Jul, 2008

I still cannot redirect my root domain to my sub folder

SEO Dubai » 23. Jul, 2008
Constantinos Kouloumbris » 23. Jul, 2008

[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.

Constantinos Koloumrbis 2004-2011 ©