Welcome Guest, Not a member yet? Register   Sign In
Rewriting image directory for a dynamic subdomain
#1

[eluser]johncook[/eluser]
I'm having htaccess issues. I want to rewrite subdomains so that:

http://subdomain.domain.com/ rewrites to http://www.domain.com/page.php

I also want to rewrite the subdomain's image directory into a subdirectory in the main images directory. For example:

http://subdomain.domain.com/images/pic.jpg rewrites to http://www.domain.com/images/subdomain/pic.jpg

To do this, I used the following htaccess code:

Code:
# rewrite any subdomain non-images to page.php
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9_-]+)\.domain\.com [NC]
RewriteCond %{REQUEST_URI} !\.(gif|jpg|png)$ [NC]
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*) page.php?%1 [L]

# rewrite any subdomain images
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9_-]+)\.domain\.com [NC]
RewriteRule ^images/(.*) images/%1/$1 [L]

Any html or php pages redirect fine but any jpg or gif files generate an Internal Server Error. Any tips on what I did wrong?
#2

[eluser]TheFuzzy0ne[/eluser]
I'm quite certain it's got something to do with the %1's you've added. I'm pretty sure the first one should be $1, Not too sure about the second one.
#3

[eluser]johncook[/eluser]
The %1 is meant to grab the subdomain from the preceding RewriteCond rule. But even if I leave the %1 out, there's weirdness. I tried rewriting to images/$1 and it works okay. Then I tried manually rewriting to a subdirectory in the images directory. Eg - images/test/$1 and it gave an Internal Server Error. So it almost seems like rewriting to a subdirectory in images is what causes the problem. Not sure why though.
#4

[eluser]TheFuzzy0ne[/eluser]
I'm no expert, but perhaps something like this might work:
Code:
# rewrite any subdomain non-images to page.php
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9_-]+)\.domain\.com [NC]
RewriteCond %{REQUEST_URI} !\.(gif|jpg|png)$ [NC]
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*) page.php?$1 [L] #I think this might be where the error lies.

# rewrite any subdomain images
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9_-]+)\.domain\.com [NC]
RewriteRule ^images/(.*) images/%1/$1 [L]

I really need to start reading up on mod_rewrite...
#5

[eluser]johncook[/eluser]
Thanks for the reply. I don't think the error is caused in the RewriteRule ^(.*) page.php?#1. All that does is grab the query string from the original URL and append it to page.php (#1 grabs content from the previous RewriteCond rule). Plus through testing, I've worked out that that RewriteRule is not being applied to images - the "RewriteCond %{REQUEST_URI} !\.(gif|jpg|png)$" condition eliminates images from this rule.

But I did try your code just in case - still got the server error. My guess is the rewrite rule "RewriteRule ^images/(.*) images/%1/$1" is causing an infinite loop (eg - subdomain.domain.com/images/pic.jpg is getting written to subdomain.domain.com/images/subdomain/pic.jpg and so on...). But I don't know how to stop that from happening.
#6

[eluser]TheFuzzy0ne[/eluser]
The way to test would be to comment out the last line, and see what happens.
#7

[eluser]johncook[/eluser]
That's the weird thing. If I comment out the last line, it now rewrites any subdomain image into the main image directory. Eg -

subdomain.domain.com/images/pic.jpg >>> www.domain.com/images/pic.jpg

But I don't have any rewrite rule that would do that. I don't get how that happens.
#8

[eluser]TheFuzzy0ne[/eluser]
That's covered by the first rule. If the file extension is gif, jpg or png, the request is not routed to the index.php (via CodeIgniter).
#9

[eluser]johncook[/eluser]
I get that. But why is it rewritten to http://www.domain.com/images/pic.jpg? I would've thought it would just try to find subdomain.domain.com/images/pic.jpg and failing that, give a 404 error.
#10

[eluser]TheFuzzy0ne[/eluser]
Oh I see. Is it possible you have a ninja htaccess file stealthily hiding in one of your other subdirectories, waiting to pounce on unsuspecting URIs with its cat-like reflexes?




Theme © iAndrew 2016 - Forum software by © MyBB