Page 1 of 1

htaccess mod rewrite for subdomains

Posted: Wed Mar 11, 2009 4:32 pm
by chrisclements
I have a wordpress MU blog set up on my site in a sub directory http://www.sitename.com/blog

I set it up so that each new blog gets a subdomain. So each blog looks like this username.sitename.com/blog

I had to set up an a record that was *.sitename.com in order to make this work.

each subdomain also has a unique index page that is not part of the wordpress blog.

Each index page is located at http://www.sitename.com/username/index.php

So what I need to do is when someone goes to username.sitename.com it will show http://www.sitename.com/username/index.php as the page, but the url will stay at username.sitename.com

I need what I should put in the htaccess file to make this work correctly.

please help....

Chris

Re: htaccess mod rewrite for subdomains

Posted: Wed Mar 11, 2009 5:29 pm
by semlar

Code: Select all

rewritecond %{http_host} ^([^\.]+)\.example\.com$
rewritecond /home/example/www/%1/ -d
rewriterule ^(.*)$ /home/example/www/%1/$1 [qsa,l]
Where /home/example/www/ is the file-system path to the directory.

Re: htaccess mod rewrite for subdomains

Posted: Wed Mar 11, 2009 6:10 pm
by chrisclements
RewriteCond %{HTTP_HOST} ^arizona.sitename.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.arizona.sitename.com$
RewriteRule ^(.*)$ http://www.sitename.com/arizona/$1 [R=301,L]

This is what I have now and it works for the redirect, however http://www.sitename.com/arizona/ shows up in the address bar of the browser.

I need arizona.sitename.com to stay in the address bar in the browser.

Re: htaccess mod rewrite for subdomains

Posted: Wed Mar 11, 2009 6:16 pm
by semlar
r=301 means "do a 301 redirect", that will change the url in the address bar. On top of that, specifying an absolute url like that causes a redirect.

You have to use a file-system path instead.

Re: htaccess mod rewrite for subdomains

Posted: Wed Mar 11, 2009 6:36 pm
by chrisclements
Sorry I am really new at this stuff. Here is what I have right now and it shows my index page for the main site

Options +FollowSymlinks
RewriteEngine on

rewritecond %{http_host} ^([^\.]+)\.yahoo\.com$
rewritecond /home/yahoo/httpdocs/%1/ -d
rewriterule ^(.*)$ /home/yahoo/httpdocs/%1/$1 [qsa,l]

Sorry, if you could spell it out for me I would be great full.

Chris

Re: htaccess mod rewrite for subdomains

Posted: Wed Mar 11, 2009 8:01 pm
by semlar
Actually I realized this might be an infinite loop, depending on your server setup.

You can just do it like this if you have the subdomains pointed at the containing folder (sub.example.com being the same as example.com):

Code: Select all

RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(?!www\.)([^\.]+)\.
RewriteRule ^(.*)$ /%1/$1 [QSA,L]

Re: htaccess mod rewrite for subdomains

Posted: Wed Mar 11, 2009 8:37 pm
by chrisclements
here is exactly what I am trying to do.

I need

arizona.mysite.com

to show the index file http://www.mysite.com/states/arizona/index.php

and

arkansas.mysite.com

to show the index file http://www.mysite.com/states/arkansas/index.php

and so on for all of the states.

Re: htaccess mod rewrite for subdomains

Posted: Wed Mar 11, 2009 10:33 pm
by semlar
Alright, since you only want it to redirect to index.php, and it's in a subdirectory, you would change it to this..

Code: Select all

RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(?!www\.)([^\.]+)\.
RewriteCond %{DOCUMENT_ROOT}/states/%1 -d
RewriteRule .* /states/%1/index.php [L]
The first condition checks that it hasn't already redirected, the next one asserts that your address doesn't start with www and creates a backreference to the subdomain, the next one checks that the directory exists, and the last line redirects everything to /states/(subdomain)/index.php.

If it doesn't work for you post a rewritelog entry.

Re: htaccess mod rewrite for subdomains

Posted: Wed Mar 11, 2009 11:28 pm
by chrisclements
THANK YOU! it works!

Re: htaccess mod rewrite for subdomains

Posted: Fri Mar 13, 2009 5:13 pm
by chrisclements
got a problem.

the htaccess you gave me works but is conflicting with my wordpress MU htaccess. which is

php_flag register_globals 0
php_flag display_errors 0

RewriteEngine On
RewriteBase /results/

#uploaded files
RewriteRule ^(.*/)?files/$ index.php [L]
RewriteCond %{REQUEST_URI} !.*wp-content/plugins.*
RewriteRule ^(.*/)?files/(.*) wp-content/blogs.php?file=$2 [L]

# add a trailing slash to /wp-admin
RewriteCond %{REQUEST_URI} ^.*/wp-admin$
RewriteRule ^(.+)$ $1/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-.*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]

<IfModule mod_security.c>
<Files async-upload.php>
SecFilterEngine Off
SecFilterScanPOST Off
</Files>
</IfModule>

What can I do?