Forum Moderators: phranque

Message Too Old, No Replies

Subdomains, rewrite & htaccess help!

I am trying to rewrite both the subdomain and variables...

         

mbredel

4:27 pm on Oct 1, 2009 (gmt 0)

10+ Year Member



Hey there!

So recently I was able to create a htaccess rewrite with the subdomain as my variable as follows:

RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com
RewriteRule .* http://example.com/index.php?affname=%1 [L,R]

This is working great! (I did need to create a wildcard A Record in my DNS entries pointing back to my IP).

Now I also want to be able to add variables beyond the root and pass it, but I can't figure this out. For example:

http://Joe.example.com/Jane

would redirect to:

http://example.com/index.php?affname=Joe&var2=Jane

Can I do this? (If so, HOW?)

cheers...matt

[edited by: jdMorgan at 4:49 pm (utc) on Oct. 1, 2009]
[edit reason] example.com [/edit]

jdMorgan

4:47 pm on Oct 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



By capturing the 'path' in the RewriteRule, and back-referencing it as well:

RewriteCond $1 !^index\.php$
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^(.*)$ /index.php?affname=%1&path=$1 [L]

Note that this is an internal rewrite, as you very likely do not want to expose your script filepath by doing an external redirect... security issues and search ranking results would be bad.

Jim

mbredel

6:57 pm on Oct 1, 2009 (gmt 0)

10+ Year Member



Hey Jim,

That works great! Thanks a lot! You are a master at this...cheers...matt

P.S. I assume the security risk you are talking about concerns if I am sending to an external site, which I am not. The entire rewrite is on the same domain. (I assume that is what you mean).

jdMorgan

7:08 pm on Oct 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, the problem is that if you expoe "index.php?affname=xyz&path=/whatever" by publishing it as a URL in an external redirect, then that is what everybody will see. This will allow those who might be inclined to try all sorts of interesting 'tricks' by directly-typing-in that URL, and toying with the parameters...

A third, optional step would be to now redirect any direct client requests for "index.php?affname=xyz&path=/whatever" back to the canonical URL, "affname.example.com/whatever" in order to avoid duplicate content problems (the same content served in response to two or more URLs).

Jim