Forum Moderators: phranque

Message Too Old, No Replies

.htaccess rewrite rule w/ php parameters issue

.htaccess rewrite rule w/ php parameters issue

         

Bomar

12:35 pm on Jun 13, 2004 (gmt 0)

10+ Year Member



Hello,

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

jdMorgan

2:33 pm on Jun 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Bomar,

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]

This causes the value assigned to userid to be taken (back-referenced) from the subdomain match in the second RewriteCond, instead of from the requested page in the RewriteRule itself. ("$1" refers to the first parenthesized subpattern in the current RewriteRule, while "%1" refers to the first parenthesized subpattern in the last RewriteCond that matched for this rule, so I believe you want %1, not $1).

Refs:
Apache mod_rewrite documentation [httpd.apache.org]
Apache URL Rewriting Guide [httpd.apache.org]
Regular Expressions Tutorial [etext.lib.virginia.edu]

Jim

Bomar

3:00 pm on Jun 13, 2004 (gmt 0)

10+ Year Member



Hello 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 :)

jdMorgan

3:40 pm on Jun 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your code looks fine, so check your server error log - it will contain specific error info.
To overcome the missing space annoyance, use two spaces before submitting the post.

Jim

Bomar

4:17 pm on Jun 13, 2004 (gmt 0)

10+ Year Member



Hmm...

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

jdMorgan

4:41 pm on Jun 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Using ^([^.]+)\.www\.example\.com as the RewriteCond pattern as you did is better, because it's slightly faster. Also, using "+" instead of "*" means that it will reject blank subdomains, and the rewrite will not happen.

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

Bomar

5:15 pm on Jun 13, 2004 (gmt 0)

10+ Year Member



Hi again,

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) :)

jdMorgan

6:47 pm on Jun 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can exclude the image dirs if you like, or exclude the image filetypes using

RewriteCond %{REQUEST_URI} !^/(imagedir¦imagedir2¦imagedir3)/

- or -
 RewriteCond %{REQUEST_URI} !\.(gif¦jp?eg¦png)$ 

etc.

You could also do it in the RewriteRule itself, e.g.


RewriteRule !\.(gif¦jp?eg¦png)$ /index.php?userid=%1 [L]

Replace the broken pipe "¦" characters with solid pipes before use! And always make sure that the last RewriteCond is the one that grabs the subdomain as shown in msg #6 above, otherwise you risk breaking the back-reference. The %1 in the RewriteRule always refers to the first parenthesized subpattern in the *last* RewriteCond that matches.

Jim