htaccess mod rewrite for subdomains

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chrisclements
Forum Newbie
Posts: 6
Joined: Wed Mar 11, 2009 4:15 pm

htaccess mod rewrite for subdomains

Post 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
semlar
Forum Commoner
Posts: 61
Joined: Fri Feb 20, 2009 10:45 pm

Re: htaccess mod rewrite for subdomains

Post 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.
Last edited by semlar on Wed Mar 11, 2009 6:13 pm, edited 1 time in total.
chrisclements
Forum Newbie
Posts: 6
Joined: Wed Mar 11, 2009 4:15 pm

Re: htaccess mod rewrite for subdomains

Post 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.
semlar
Forum Commoner
Posts: 61
Joined: Fri Feb 20, 2009 10:45 pm

Re: htaccess mod rewrite for subdomains

Post 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.
chrisclements
Forum Newbie
Posts: 6
Joined: Wed Mar 11, 2009 4:15 pm

Re: htaccess mod rewrite for subdomains

Post 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
semlar
Forum Commoner
Posts: 61
Joined: Fri Feb 20, 2009 10:45 pm

Re: htaccess mod rewrite for subdomains

Post 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]
chrisclements
Forum Newbie
Posts: 6
Joined: Wed Mar 11, 2009 4:15 pm

Re: htaccess mod rewrite for subdomains

Post 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.
semlar
Forum Commoner
Posts: 61
Joined: Fri Feb 20, 2009 10:45 pm

Re: htaccess mod rewrite for subdomains

Post 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.
chrisclements
Forum Newbie
Posts: 6
Joined: Wed Mar 11, 2009 4:15 pm

Re: htaccess mod rewrite for subdomains

Post by chrisclements »

THANK YOU! it works!
chrisclements
Forum Newbie
Posts: 6
Joined: Wed Mar 11, 2009 4:15 pm

Re: htaccess mod rewrite for subdomains

Post 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?
Post Reply