Forum Moderators: phranque
I have the following .htaccess on my server:
RewriteEngine on
RewriteCond %{HTTP_HOST}!^(www\.)?mydomain\.com [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com [NC]
RewriteRule ^(.*)$ /index.php?userid=$1 [L]
The PHP script looks as follows:
<?php
if ( phpversion() >= "4.2.0")
{
extract($_POST);
extract($_GET);
} if (!$userid) {
echo "No userid sent";
} else {
echo "userid sent - $userid";
}
?>
For some reason it doesnt work as the userid is not parsed (no idea why)
What I try to do:
Based on the subdomain a users profile shows up.
Any idea how to solve this one? Btw. it doesn't matter if somebody knows a redirect or rewrite solution - both would be just great :)
Thanks
Welcome to WebmasterWorld [webmasterworld.com]!
If you want to base the value of variable "userid" on the requested subdomain, change your RewriteRule to:
RewriteRule .* /index.php?userid[b]=%1[/b] [L]
Refs:
Apache mod_rewrite documentation [httpd.apache.org]
Apache URL Rewriting Guide [httpd.apache.org]
Regular Expressions Tutorial [etext.lib.virginia.edu]
Jim
first of all: thanks for your time to reply :)
It's great to see that people actually care about issues :)
I tried your solution but got an error 500 now.
My current .htaccess code (as you suggested) is:
RewriteEngine On
RewriteCond %{HTTP_HOST}!^(www\.)?myurl\.com [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.myurl\.com [NC]
RewriteRule .* /index.php?userid=%1 [L]
Any idea why www. and myurl.com work but if I try something.myurl.com I get the 500 code.
Thanks again
Edit: I cannot post a space in the first cond for some reason (edit didnt work) so it's not a typo :)
I tried yet another way:
RewriteEngine On
RewriteCond %{HTTP_HOST}!^www\.myurl\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.myurl\.com [NC]
RewriteRule ^(.*)$ /index.php?userid=%1 [L,R]
This works (forwards to index.php?userid=whatever)
however... if I try to make it like this:
RewriteEngine On
RewriteCond %{HTTP_HOST}!^www\.myurl\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.myurl\.com [NC]
RewriteRule ^(.*)$ /index.php?userid=%1 [L]
then I get a 500 with no entries in the error log for some reason... I am really lost ...
Any other ideas?
Thanks
You don't need the parenthesis in the RewriteRule pattern, because you are not back-referencing the requested page URL-path. Leave them out unless they are needed.
Another question I have is "where is your index.php script located?" Using a path of "/index/php" indicates that the script must be in the document root of your site - the "home page" directory. Since the only difference in the two code snippets is that the first uses an external redirect, you may have some funky configuration problem with aliases or symlinks that is misdirecting internal rewrites.
You might also try throwing in another RewriteCond to prevent looping on internal subrequests:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com [NC]
RewriteRule .* /index.php?userid=%1 [L]
(Yeah, I'm guessing, too! This should not be this hard to get working. Keep experimenting and looking for clues in the error logs.)
Jim
your solution worked out, which is a great thing :) However... we have a new problem now *hides*
The subdomains work as they should but all images are "broken" aka the lovely red x.
I really feel bad but do you have any clue why this happens? Maybe I have to exclude the image dirs or so?
(I am pretty new to rewrite rules but I guess you already know) :)
RewriteCond %{REQUEST_URI} !^/(imagedir¦imagedir2¦imagedir3)/ RewriteCond %{REQUEST_URI} !\.(gif¦jp?eg¦png)$ You could also do it in the RewriteRule itself, e.g.
RewriteRule !\.(gif¦jp?eg¦png)$ /index.php?userid=%1 [L]
Jim