Forum Moderators: phranque

Message Too Old, No Replies

I know, yet another mod_rewrite question. :)

         

georgesgabereau

11:16 pm on Nov 23, 2005 (gmt 0)

10+ Year Member



Hey guys,

I just registered today after having used many previous threads from this site as guidance for mod_rewrite.

However, none of the threads showed me exactly how to do what I wanted and I read the two-part mod_rewrite tutorial on this site to no avail. Thus, I come for helo directly...

I would like this:

http://sub.example.com/gallery/request.php

to become:

http://example.com/gallery/sub/request.php

Now, after having read a few threads here and going through the tutorial, I've come up with this:


RewriteCond %{HTTP_HOST} ^([^/]+).(.+)
RewriteCond %{REQUEST_URI} ^/gallery/(.*)
RewriteRule ([^.]+)(\.example.comt)(.*) gallery/$1/$3 [L,R,QSA]

Now, you all look and that and say, "Oh man, that's not gonna work..." :)

The part I'm missing is how to pull out the subdomain as a variable to be used in the URL. How does one do what I want to do?

Thanks in advance...

[edited by: jdMorgan at 11:39 pm (utc) on Nov. 23, 2005]
[edit reason] Example.com [/edit]

jdMorgan

11:37 pm on Nov 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I would like: http://sub.example.com/gallery/request.php to become http://example.com/gallery/sub/request.php

How does one do what I want to do?

Use %1 through %9 in the RewriteRule substitution to back-reference the matched contents of parenthesized sub-expressions in RewriteCond.

For use in .htaccess:


RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteRule ^gallery/request\.php$ /gallery/%1/request.php [L]

For httpd.conf:

RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteRule ^/gallery/request\.php$ /gallery/%1/request.php [L]

In both cases, the first RewriteCond prevents "www" in the domain name from being treated as a subdirectory, and the second RewriteCond allows an optional "www" in addition to the required subdomain name.

An internal rewrite is used rather than an external redirect, so the user always sees the URL he entered, and does not see the /gallery/sub/ path. [R] should not be used in this case, and [QSA] is not needed, since you are not appending additional query string parameters.

Jim

georgesgabereau

3:35 am on Nov 24, 2005 (gmt 0)

10+ Year Member



Ok, now that I read that, it makes more sense... the rewriterule is something I really didn't get. Seeing it like this kind of helps me see the relation of Cond to Rule.

So, if I wanted to match ALL subdomains, including www, I would do the following, right?

RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com
RewriteRule ^gallery/request\.php$ /gallery/%1/request.php [L]

jdMorgan

4:44 am on Nov 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For efficiency's sake, you should start-anchor the domain name:

RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^gallery/request\.php$ /gallery/%1/request.php [L]

This will accept "www.example.com" or "abc.example.com". It will not accept "www.abc.example.com".

Jim

georgesgabereau

5:55 am on Nov 24, 2005 (gmt 0)

10+ Year Member



Hi Jim,

Thanks a lot for the help so far.

One more thing... forget the request.php part. Would this work for any call inside /gallery/?

RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^gallery/(.*)$ /gallery/%1/$1 [L]

If I understand correctly, this will take any text after the / and turn it into $1 which I can then pass forward to the scripts in the appropriate subdomain dir. Yes?

jdMorgan

8:22 pm on Nov 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, it won't work, because the 'old' and 'new' URLs are now no longer sufficiently unique to avoid an infinite rewriting loop. You will need to 'tag' the new URL in some way so that it can be excluded from being rewritten to itself (mod_rewrite behaves recursively in .htaccess).

The easiest way to avoid this is to rewrite sub.example.com/lmn/abc.xyz to /lmn/s_sub/abc.xyz, and then test for the subdirectory prefix "s_" to avoid looping, using
RewriteCond %{REQUEST_URI} !^/lmn/s_.

Note that "s_" can reqally be anything -- as long as it is sufficiently unique to never appear in the subdomain name itself or in another subdirectory path.

Jim