Forum Moderators: phranque

Message Too Old, No Replies

Redirect a domain to a subfolder

Tried searching...

         

apauto

2:31 am on Dec 20, 2008 (gmt 0)

10+ Year Member



Hi guys...

I tried searching, and found a few threads, but this one seemed to be what I needed:

[webmasterworld.com...]

Turns out, it's not working.

All I need to do is to redirect my entire site:

www.example.com

to

www.example.com/folder

What's the best way to do this with .htaccess? It seems simple enough, but for some reason it's not working :)

Thanks everyone, and happy holidays!

aschepelern

3:30 pm on Dec 20, 2008 (gmt 0)

10+ Year Member



Hi,

I really need this code too - just spend a day pasting something that doesn't do the trick. Need to rewrite:

example.com & www.example.com

->

example.com/subfolder & www.example.com/subfolder

MAIN PROBLEM is that current rewrite doesn't initially show /subfolder in url.

Here is my current htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /blog/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ subfolder/index.php [L]

This would really make my holliday!

Thanks in advance.

gergoe

5:06 pm on Dec 20, 2008 (gmt 0)

10+ Year Member



The solution is pretty simple, you would not even need mod_rewrite, nor htaccess file for that. But to stick to mod_rewrite (Jim loves that), then the following will do the trick:

RewriteEngine on 
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^$ [%{HTTP_HOST}...] [R,L]

This redirects requests without any path information (that's http://www.example.com/) into the index.php located in the subfolder directory.

If you have some mod_rewrite directives in the htaccess file, then make sure this is placed on top of the rules, so in the case of aschepelern's post, it should look like this:

RewriteEngine on 
#
# Redirect into subfolder when no path information in request
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^$ [%{HTTP_HOST}...] [R,L]
#
# Rewrite into blog when request is not for anything in subfolder
# and when the request is not for an existing file or directory.
# Actually the first condition (the subfolder one) is not really needed, because
# subfolder exists (based on your example), so the second two conditions
# would take care of it anyway.
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /blog/$1 [L]

apauto

7:06 pm on Dec 20, 2008 (gmt 0)

10+ Year Member



gergoe, it redirects, but it's not considered a 301 redirect like this ,right?

g1smd

7:59 pm on Dec 20, 2008 (gmt 0)

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



That's a 302 redirect, and likely not what you wanted.

Change it to [R=301,L] to make it into a 301 redirect.

One point. In the questions above, one person asked for a rewrite and the other asked for a redirect.

The redirect makes the browser ask for a new and different URL when a particular URL (or set of URLs) is/are asked for.

A rewrite ensures the same old external URL still works for accessing some content after that content was physically placed in a different place inside the server.

You need to be very clear which one of those two things you actually want.

apauto

8:12 pm on Dec 20, 2008 (gmt 0)

10+ Year Member



I'm asking for the redirect. The thread hijacker is asking for something else :)

g1smd

8:34 pm on Dec 20, 2008 (gmt 0)

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



It's generally bad form to redirect the root URL of a site to a deeper internal URL.

If you're having to move the stuff around on the server, you'd normally employ a rewrite so that users use the same old URLs to access that content. They would never know you had changed the internal way the site operated.

Maybe you could go into a bit more detail why you think you need to redirect to new URLs? (I'm not saying you're wrong, but rather that what you think you need, might be better done some other way).

aschepelern

10:36 pm on Dec 20, 2008 (gmt 0)

10+ Year Member



Hi, thanks for the replies so far and sorry for hijacking the thread. I'll be sure to start my own if i have any more problems...

apauto

8:18 am on Dec 21, 2008 (gmt 0)

10+ Year Member



g1smd, thanks for your reply. The problem is the entire site is dedicated right now to blue widgets. The URLs are like this:

www.example.com/blue-widget-1.html
www.example.com/blue-widget-2.html
www.example.com/blue-widget-3.html

I want all of the color widgets, so I want to change it to:

www.example.com/blue/widget-1.html
www.example.com/red/widget-1.html
www.example.com/black/widget-1.html

the index page of the site would ask users what color widgets they want

But before I add the other colors, I need to redirect everything on the site now into one folder, let google get the changes, and then add the other colors.

What do you suggest? Does my idea make sense?

Thanks