Forum Moderators: phranque

Message Too Old, No Replies

Rewriting to /home with main site, but not virtual subdomain

mod_rewrite

         

jncr

1:40 am on Nov 27, 2007 (gmt 0)

10+ Year Member



My server is configured to have a wildcard subdomain *.example.com. This means if someone types anything.example.com it shows example.com without redirecting.

I want example.com to show example.com/home, but anything.example.com to stay at example.com.

Any help will be appreciated.

Here is my current .htaccess:


Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

This just forces no www.

Edit:
I have tried adding

RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*)$ http://example.com/home/$1 [L]

to the end of the htaccess but it just gives an internal service error.

[edited by: jdMorgan at 2:48 pm (utc) on Nov. 27, 2007]
[edit reason] example.com [/edit]

jdMorgan

2:57 pm on Nov 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your second rule will loop, redirecting / to /home/ to /home/home/, to /home/home/home/
So you must add a RewriteCond to explicitly exclude /home from being redirected:

Options +FollowSymLinks +Indexes
RewriteEngine on
RewriteBase /
#
# Externally redirect to canonical example.com domain
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]
#
# Externally redirect to "/home" subdirectory
RewriteCond %{HTTP_HOST} ^example\.com
RewriteCond $1 !^home/
RewriteRule (.*) http://example.com/home/$1 [R=301,L]

It is "non-optimal" to have your home page URL pointed to somewhere other than the root of your site, but if you feel that this is necessary despite the non-standard URL and the difficulties this may cause with search engine indexing, then the code above should work.

Personally, I would recommend an internal rewrite instead of an external redirect if your goal is simply to organize the the site's files into a subdirectory. That is, the URL remains example.com/ while the files are actually located at the filepath /users/domain/html/home (example only, actual filepath depends on your server config).

Jim

jncr

6:10 pm on Nov 27, 2007 (gmt 0)

10+ Year Member



Thanks again Jim!

Yes that was the problem that I was getting. It redirected to domain.com/home/home/home... ect.