Forum Moderators: phranque

Message Too Old, No Replies

Direct sub-domain redirect using .htaccess

         

NeedExpertHelp

7:33 pm on Oct 8, 2009 (gmt 0)

10+ Year Member



Hello again everyone.

I have a very simple question.

How do I redirect the following in .htaccess:

my.domain.com

to

domain.com/my.php

?

I want it to redirect no matter what comes after my.domain.com (e.g. my.domain.com/whatever) and if a user goes to my.domain.com/whatever, I want my.php to be able to read "whatever" from the REQUEST_URI parameter in PHP.

Also, I only want the rewrite to happen if the subdomain is "my".

What is the simplest and easiest way to accomplish this?

It sounds very straightforward but I can't get it to work based on similar examples I've seen here.

Please note that I currently use .htaccess to redirect the www to non-www.

Thanks!

jdMorgan

10:43 pm on Oct 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please post your best-effort code as a basis for discussion. The more specific your question, and the more of the solution that you find yourself, the better the responses here will be.

Our Apache Forum Charter and Library both contain useful resources.

Thanks,
Jim

NeedExpertHelp

11:19 pm on Oct 8, 2009 (gmt 0)

10+ Year Member



I thought this was straightforward enough that it did not need my embarrassing "best efforts" as a starting point. :) I was obviously wrong (my .htaccess skills are pitiful, so what I sometimes think is easy, is clearly not, like in this case).

Here are a few examples I've tried before posting here (it's been pretty much trial and error based on similar examples I've seen on this forum, but nothing has worked so far):

-----
RewriteCond %{HTTP_HOST} ^my\.domain\.com\(.*)
RewriteRule .* [domain.com...] [R=301,L]
-----

-----
RewriteCond %{HTTP_HOST}.
RewriteCond %{HTTP_HOST} ^my\.domain\.com
RewriteRule (.*) [domain.com...] [R=301,L]
-----

-----
RewriteCond %{HTTP_HOST} ^my\.domain\.com
RewriteRule ^(.*)$ /my.php [L]
-----

-----
RewriteRule ^my\.domain\.com\(.*) /my.php [L]
-----

As you can probably tell from the above, I have no idea what I'm doing and am simply "guessing" based on examples I have seen. That's why I need expert help.

Thanks.

jdMorgan

1:21 am on Oct 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The mod_rewrite documentation linked from our Charter is a must-read -- even if you 'don't get it' all the first time through. We don't like to 'just hand out code here' for many reasons, but one of them is this: If you don't fully understand the code you put on your server, it is very easy to make one tiny mistake that will utterly trash your search results, and might even put you out of business. So, we focus more on 'education' and 'guidance' here, instead of just blatting out 'quick pre-coded solutions.' We also don't have enough volunteers to write everybody's code for them, so the 'post your code' requirement shows at least some minimum level of effort.

The most likely problem is that you've told Apache mod_rewrite to redirect *all* requests to /my.php. So a request comes in for xyz.abc, and the server issues a redirect to the client, asking it *not telling, asking* to issue a second HTTP request, for example.com/my.php. This redirect response from the server terminates the current HTTP transaction.

Now the client will probably issue the second request, asking for example.com/my.php, and that request will be processed by the same RewriteRules as the first request was...

So, the request for my.php gets redirected as well... back to example.com/my.php. That's an infinite loop!

Now, backing off one full step here, I have to ask, "Why are you trying to redirect the client to example.com/my.php in your first two examples? With a redirect, the "example.com/my.php" URL will show in the client's address bar, and the originally-requested 'page name' will be lost. Your site will become a one-page site, with no images, no css, no javascript, no media files, no robots.txt, no sitemap.xml, no favicon.ico, etc. The only 'page' that will be available, even if you fix the loop described above, will be example.com/my.php.

I think you want an internal rewrite as shown in your second two rules instead, so that all requested URL-paths are rewritten to (i.e. "passed to") the *file* /my.php instead. That is, an external redirect is a URL-to-URL translation, requiring the client to take action (and changing the browser address bar as a result), whereas an internal rewrite is a URL-to-filename translation, and does not 'inform' the client in any way.

If that sounds more like what you're after, then try:


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

The first RewriteCond prevents the 'infinite loop' previously discussed.

If you are looking to rewrite <any-subdomain>.example.com to /my.php, or to rewrite <any-subdomain>.example.com to /<any-subdomain>.php, then that is another question, and the answer will likely affect your whole 'plan' for assigning subdomains to scripts, so please re-post if that's the case.

Jim

[edited by: jdMorgan at 4:21 am (utc) on Oct. 9, 2009]

TheMadScientist

3:54 am on Oct 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I thought this was straightforward enough that it did not need my embarrassing "best efforts" as a starting point. :) I was obviously wrong (my .htaccess skills are pitiful, so what I sometimes think is easy, is clearly not, like in this case).

We all started there, and Mod_Rewrite is possibly one of the most complex and confusing 'languages' you will work with... I work with both PHP and JavaScript also and for almost everything there is a predefined function and the need for regular expressions is limited.

Mod_Rewrite is the exact opposite.

Everything is a regular expression (pattern match) and there are no predefined functions or rules to choose from, because it has to be site specific, even for what seems to be a simple solution, so don't feel bad about it or talk about your efforts being pitiful, at least you made a few and were trying some different things, which is more that many people can claim...