Forum Moderators: phranque
I was looking for a way to redirect
subdomain.domain.com/request
to
www.domain.com/index.php?s=subdomain&r=request
Thanks to this post (http://www.webmasterworld.com/forum92/138.htm) I got pretty far, but there is still one thing.
I use this as my .htaccess file:
# Turn rewriteEngine on
rewriteEngine On
# rewrite only when Host is not empty
rewriteCond %{HTTP_HOST} !^$
# rewrite only when Host is not main domain
rewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
# Extract subdomain and first path element
rewriteCond %{HTTP_HOST}<->%{REQUEST_URI} ^(www\.)?([^.]+).*<->/([^/]+) [NC]
# rewrite only when subdomain not equal to first path element
rewriteCond %2<->%3 !^(.*)<->\1$ [NC]
# rewrite to index.php?s=subdomain&r=request
rewriteRule ^(.+) index.php?s=%2&r=$1 [L]
I have made an index.php file to show what $_POST-values it gave me back:
<?php
echo '<pre>';
print_r($_GET);
echo '</pre>';
?>
And if i type subdomain.domain.com/request, the index.php gives me this:
Array
(
[s] => subdomain
[r] => index.php
)
Array
(
[s] => subdomain
[r] => request
)
so, what am I still doing wrong?
Thank you in advance
Just add a RewriteCond to stop recursion:
# Turn rewriteEngine on
RewriteEngine on
#
# Rewrite only if requested URI is not index.php
[b]RewriteCond $1 !index\.php$[/b]
# rewrite only when Host is non-empty
RewriteCond %{HTTP_HOST} .
# rewrite only when Host is not main domain
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
# Extract subdomain and first path element
RewriteCond %{HTTP_HOST}<->%{REQUEST_URI} ^(www\.)?([^.]+).*<->/([^/]+) [NC]
# rewrite only when subdomain not equal to first path element
RewriteCond %2<->%3 !^(.*)<->\1$ [NC]
# rewrite to index.php?s=subdomain&r=request
RewriteRule (.+) index.php?s=%2&r=$1 [L]
Jim
but I have another question. If you type [subdomain.domain.com...] everything works. I get an array back saying
Array
(
[s] => subdomain
[r] => request
)
But, if I type [subdomain.domain.com,...] It doesn't redirect at all. My .htaccess is
<IfModule mod_rewrite.c>rewriteEngine On
RewriteCond $1 !index\.php$
rewriteCond %{HTTP_HOST} !^$
rewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
rewriteCond %{HTTP_HOST}<->%{REQUEST_URI} ^(www\.)?([^.]+).*<->/([^/]+) [NC]
rewriteCond %2<->%3 !^(.*)<->\1$ [NC]
rewriteRule ^([^/.]+)/?$ index.php?a=%2&b=$1
</IfModule>
Thank you in advance!
I changed the last line from
rewriteRule ^([^/.]+)/?$ index.php?a=%2&b=$1
rewriteRule ^(.*)/?$ index.php?a=%2&b=$1
What am I doing wrong? Thank you in advance