Forum Moderators: phranque

Message Too Old, No Replies

Redirecting a subdomain that doesn't exist to a php script

redirecting to a php script using the subdomain name as parameter

         

kenchix1

3:48 pm on Jul 26, 2005 (gmt 0)

10+ Year Member



this htaccess doesn't work and doesn't do anything. I got the idea here [webmasterworld.com...] on how to redirect the subdomain. I copy and paste the code, modify it little but it didn't work. I'm new to apache and hope somebody can help me point out my problem. below is my htaccess that I used.

thanks in advance. :)
-----------------------------------------

RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.webmasterworldsite\.dom
RewriteRule (.*) [%1.webmasterworldsite.dom...] [R=301,L]
#
RewriteCond %{REQUEST_URI}!^/mypage\.php
RewriteCond %{HTTP_HOST}!^www\.webmasterworldsite\.dom
RewriteCond %{HTTP_HOST} ^([^.]+)\.webmasterworldsite\.dom
RewriteRule ^$ /mypage.php?%1 [L]
#
RewriteCond %{HTTP_REFERER}!^http://webmasterworldsite.dom/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://webmasterworldsite.dom$ [NC]
RewriteCond %{HTTP_REFERER}!^http://webmasterworldsite2.dom/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://webmasterworldsite2.dom$ [NC]
RewriteCond %{HTTP_REFERER}!^http://www.webmasterworldsite.dom/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://www.webmasterworldsite.dom$ [NC]
RewriteCond %{HTTP_REFERER}!^http://www.webmasterworldsite2.dom/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://www.webmasterworldsite2.dom$ [NC]

RewriteRule .*\.(jpg¦jpeg¦gif¦png¦bmp)$ - [F,NC]

jdMorgan

9:10 pm on Jul 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You did not say how the code failed, but I'd suggest the following changes and clean-ups:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.example\.com
RewriteRule (.*) http://%1.example.com/$1 [R=301,L]
#
RewriteCond %{REQUEST_URI} !^/mypage\.php
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule .* /mypage.php?%1 [L]
#
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example2\.com [NC]
RewriteRule \.(jpg¦jpeg¦gif¦png¦bmp)$ - [F,NC]

I added an Options directive at the beginning. This may or may not be needed on our server. If it's not needed, it might even cause a problem, so try it with and without this line.

The pattern ^$ in your second rule has been changed to .* to allow all files in the subdomain to be rewritten.

The last two rules above replace all eight of your image-blocking rules, and avoid the problem of appended port numbers breaking your original rules.

Replace all broken pipe "¦" characters above with solid pipe characters from your keyboard before trying to use this code.

Jim

kenchix1

3:04 am on Jul 27, 2005 (gmt 0)

10+ Year Member



thank you sir! :)

but the code still didn't work. I contacted the host support and they say that mod_rewrite works on all the server. we are on a shared hosting.

I replaced the broken pipes to solid pipes and change the domain names on the script and uploaded it, nothing happened. I also tried removing the first line but still didn't work. I check the php script using mypage.php?username and the script is working.

if I type [username.example.com...] the error message is "username.example.com cannot be found, please check the name and try again."

I also tried several pattern from drbacchus rewrite cookbook but it did't work. :(

thanks for the help.

jdMorgan

3:15 am on Jul 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Going back to first principles, have you modified or added records to your DNS zone file to *define* these subdomains? And will your host recognize them and deliver them to your "account"? If not, then no amount of code in .htaccess will help.

Note that you can define them one-by-one, or simply define a wild-card subdomain and specify the IP address of your server.

Jim

kenchix1

3:31 am on Jul 27, 2005 (gmt 0)

10+ Year Member



sorry sir but I don't understand what you mean.

Going back to first principles, have you modified or added records to your DNS zone file to *define* these subdomains? And will your host recognize them and deliver them to your "account"?

where can I find the DNS zone file? I have hundreds of users, should i define each of them inside the DNS zone file that you mentioned?

thanks for the patience.

kenchix1

3:35 am on Jul 27, 2005 (gmt 0)

10+ Year Member



btw, Im using cpanel and it has subdomain manager, should I create a subdomain for each member?

thanks again.

jdMorgan

4:52 am on Jul 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ask your host how you can access your DNS zone file.

It is not clear whether you have any existing subdomains other than "www" defined. If you have several and you did not manually create them, then you probably don't have to worry about the zone file, because the existence of these subdomains would indicate that you've already got wild-card DNS set up. However, the error message you posted above argues against that.

If you intend to "automatically" redirect subdomains to subdirectories, don't use cPanel. Doing so would require you to manually create each subdomain and a subdirectory to host it.

You might want to try a few searches for "wild-card DNS" here on WebmasterWorld and on the Web at large. You may find a decent tutorial by doing so.

Jim

kenchix1

6:04 am on Jul 27, 2005 (gmt 0)

10+ Year Member




Ask your host how you can access your DNS zone file.

Done. I'll just wait for their reply. Thanks. :)


It is not clear whether you have any existing subdomains other than "www" defined.

I have several subdomains and I manually created it.


If you intend to "automatically" redirect subdomains to subdirectories, don't use cPanel.

ok, I already removed the subdomains.

I'll wait for the reply of my host and research on the wild-card DNS.

Many thanks! :)

kenchix1

6:42 am on Jul 27, 2005 (gmt 0)

10+ Year Member



I received the email from the host, they said they already setup the wild-card dns on that domain. After loading the .htaccess again a new error occur it says

403 Forbidden
You don't have permission to access / on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Apache/1.3.33 Server at www.**********.com Port 80

jdMorgan

1:18 pm on Jul 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sounds like it's time to restore your back-up config files, or ask your host to do so... :(

Jim

kenchix1

2:34 pm on Jul 27, 2005 (gmt 0)

10+ Year Member



I had the original file of .htaccess. The error I mentioned occurs when I type username.example.dom on the browser but if I type www.example.dom or example.dom it works fine.

It seems that the host don't give rights on the dns zone file and they're the one who setup the wild-card dns (as they say). I tried using several pattern again up to the point that the site gives "too many redirection" error but I can't really do it and restored the original back to your script that you gave.

The host also said that I should wait for the propagation of the DNS and I should check the mod_rewrite script if it is working properly, but I don't know how to check it (use it on another site?)

thanks sir!

jdMorgan

2:51 pm on Jul 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you are still getting the "username.example.com cannot be found, please check the name and try again" error message, then you'll need to wait (sometimes up to three days) for the DNS changes to propagate. This is assuming that they just made the changes to your DNS zone file.

Looking at the code, the second and third rulesets should be exchanged as shown below. This would not have caused the error you are seeing, but will prevent image requests from invalid referrers from being rewritten to your mypage.php script. You'll also need to allow for blank referrers, as discussed many times in hotlinking threads in this forum.


Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.example\.com
RewriteRule (.*) http://%1.example.com/$1 [R=301,L]
#
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example2\.com [NC]
RewriteRule \.(jpe?g¦gif¦png¦bmp)$ - [F,NC]
#
RewriteCond %{REQUEST_URI} !^/mypage\.php
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule .* /mypage.php?%1 [L]

To emphasize, be aware that *all* valid requests will be rewritten to the mypage.php script. You may want to add exclusions to the code for images, css files, external JavaScript files, robots.txt, the /w3c/p3p.xml privacy policy file, and error documents; Unless your mypage script is designed to handle these as well, I strongly recommend it. As an alternative to adding exclusions, you could also specify a list of fieltypes that you *do* want to rewrite, for example, all .html pages.

Jim

kenchix1

3:23 pm on Jul 27, 2005 (gmt 0)

10+ Year Member



I don't think its the propagation anymore because the is

Forbidden
You don't have permission to access / on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Apache/1.3.33 Server at www.********.com Port 80

I assume the "/" is the root.

I just notice that if you type
example.dom/anythinghere -> will give blank page
username.example.dom -> will give 403 and 404 error
username.example.dom/anythinghere -> will give you blank page

I'll email the host and ask if they can check the wild-card dns again.

thanks again.

kenchix1

2:37 am on Jul 28, 2005 (gmt 0)

10+ Year Member



I send the mod_rewrite code to the host and they tweaked the DNS zone file. After several attempt they told me that the error is on the mod_rewrite code and not on the server.
I modified the code several times but still, I can't get it to work. I still got 403 and 404 error. now I'm stuck. :(

kenchix1

5:35 am on Jul 28, 2005 (gmt 0)

10+ Year Member



I saw the DNS zone file and my domain has an entry of

* 14400 IN CNAME example.dom.

kenchix1

5:59 am on Jul 28, 2005 (gmt 0)

10+ Year Member



after so many many attempts, the only thing I was able to redirect to mypage.php is the "www". :(

jdMorgan

2:23 am on Jul 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest a quick test -- and it may need to be *very* quick, and done during off-peak hours:

Copy your .htaccess file to a backup, and then delete .htaccess from your top directory. Put a dirt-simple "index.html" file in your home directory if none exists -- plain HTML and no scripts.

Now access your domain, www subdomain, and other subdomains. If your host has set up your acccount properly, you should see the same index.html page for each request. If not, write them, tell them you deleted .htaccess, and it still does not work.

Jim

kenchix1

5:40 am on Jul 29, 2005 (gmt 0)

10+ Year Member



thank you very much sir for all the help.

I did just what you just said.

1.) deleted .htaccess
2.) put in index.html
3.) access bbb.example.com - Error 403
4.) access www.example.com - blank html appeared
5.) access www.bbb.example.com - Error 403

I put back the original index.php, now my site runs without it.

but the username.example.com still got error 403.

should I report to the host about it?

thanks again.

kenchix1

5:48 am on Jul 29, 2005 (gmt 0)

10+ Year Member



btw, I already deleted all the subdomains in my site. those bbb.example.com doesn't really exist.

jdMorgan

2:37 pm on Jul 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you are getting 403's after having deleted your .htaccess, then obviously your .htaccess is not causing that problem, and this looks like a server config problem. Since you're on shared hosting, your host will have to look into that. They may have to go into httpd.conf and clean up some detritus left over by cPanel.

Jim

kenchix1

1:22 am on Jul 30, 2005 (gmt 0)

10+ Year Member



My host just emailed me and said for that redirection/mod-rewrite to work, I have to purchase an additional dedicated IP. they should have told me before.

Thank you very much again for all the help sir.

jdMorgan

2:08 am on Jul 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I have to purchase an additional dedicated IP.

Piffle.

Jim