Forum Moderators: phranque

Message Too Old, No Replies

subdomain mod rewrite

I get the subdomain, but the subfolder doesn't work...

         

dorque

7:39 pm on Feb 20, 2008 (gmt 0)

10+ Year Member



Hello everybody,

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
)

What i would like to get is this


Array
(
[s] => subdomain
[r] => request
)

so, what am I still doing wrong?
Thank you in advance

jdMorgan

3:26 am on Feb 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You probably have an internal rewriting loop going -- The second time through, the URL is rewritten to "index.php" not matter what the original request was.

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]

Note to readers: This code works only on servers running an OS that supports POSIX 1003.2 regular expressions or later. It cannot be considered "portable," because it depends on POSIX 1003.2 "atomic back-references."

Jim

dorque

6:13 am on Feb 21, 2008 (gmt 0)

10+ Year Member



Thank you, that works great!

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!

jdMorgan

3:18 pm on Feb 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Change the RewriteRule pattern from "(.+)" to "(.*)" if you wish to accept a blank URL-path.

Jim

dorque

5:09 pm on Feb 21, 2008 (gmt 0)

10+ Year Member



Thank you so much for helping me Jim.

I changed the last line from


rewriteRule ^([^/.]+)/?$ index.php?a=%2&b=$1

to

rewriteRule ^(.*)/?$ index.php?a=%2&b=$1

And I left the rest of the lines untouched. It works perfect if I go to [subdomain.domain.com...] but when I try to get to [subdomain.domain.com,...] it still doesn't do anything.

What am I doing wrong? Thank you in advance