Welcome Guest, Not a member yet? Register   Sign In
Rewrite rule generating Internal Server Error
#1

[eluser]johncook[/eluser]
I'm trying to rewrite subdomain images:

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

So I tried grabbing the subdomain from the RewriteCond:

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

This generates an Internal Server Error. So I experimented to see what did and didn't work. First to make sure it's working at all, I tried:

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

This works fine - rewrites http://subdomain.domain.com/images/pic.jpg >>> http://www.domain.com/images/pic.jpg.

This tells me it's not an error like an infinite loop. Next I tried manually typing in the subdirectory rather than using %1:

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

Again, Internal Server Error. Any clues on what's causing the Server Error?
#2

[eluser]TheFuzzy0ne[/eluser]
Is it possible that a directory for that sub domain doesn't exist?
#3

[eluser]TheFuzzy0ne[/eluser]
One more thing. What subdomain are you using? If it's not matched by the regex, that could also explain your problem.
#4

[eluser]johncook[/eluser]
The directory does exist and does match the regex (as far as I know). An example, the URL is:
http://cavaliers.criciwiki.com/images/header.jpg

I want it to rewrite to:
http://www.criciwiki.com/images/cavaliers/header.jpg

Trying both
RewriteRule ^images/(.*)$ images/cavaliers/$1 [L]
and
RewriteRule ^images/(.*)$ images/%1/$1 [L]
gives an Internal Server Error. It seems like trying to rewrite to a subdirectory in images is what causes the problem.

For the record, rewriting to just the images directory works okay (which confirms the regex matches):
RewriteRule ^images/(.*)$ images/$1 [L]
#5

[eluser]Colin Williams[/eluser]
[[DELETED]]
#6

[eluser]TheFuzzy0ne[/eluser]
Code:
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9_-]+)\.domain\.com [NC]
RewriteRule ^images/(.*)$ /images/%1/$1 [L]

I wonder if that will work. I think that paths are somehow the issue here.
#7

[eluser]johncook[/eluser]
No, adding the forward slash at the start of the path still gives an internal server error. It does seem to be the path that's an issue. I've tried the following rewrite rule:

Code:
RewriteRule ^images/(.*)$ %1/$1 [L]

This successfully writes subdomain.domain.com/images/pic.jpg to www.domain.com/subdomain/pic.jpg

That's not the solution I was after - it needs to be in the images directory. But for some weird reason, it just doesn't want to do it.
#8

[eluser]TheFuzzy0ne[/eluser]
It's almost as if the images directory doesn't exist, can you confirm that:
a) The directory isn't in fact named "image", and
b) The directory permissions are correct.
#9

[eluser]johncook[/eluser]
Nothing as simple as typos (I wish it were that although I would be bludgeoning myself with a blunt instrument right now if it were that simple).

I know it's not a typo because if I simply remove the %1/ from the rewrite rule, it works. Eg - these rules work:
RewriteRule ^images/(.*)$ images/$1 [L]
RewriteRule ^images/(.*)$ %1/$1 [L]

These rules give a Internal Server Error:
RewriteRule ^images/(.*)$ /images/$1 [L]
RewriteRule ^images/(.*)$ images/%1/$1 [L]
RewriteRule ^images/(.*)$ images/cavaliers/$1 [L]

I just checked the permissions and both images/ and images/cavaliers/ have chmod 2777 (eg - read & write permissions are all good to go). I really appreciate all your advice - I'm on the verge of giving up on this though, it just doesn't make any sense.
#10

[eluser]TheFuzzy0ne[/eluser]
I've been meaning to learn how to write htaccess files for as long as I can remember. I guess now would be the ideal time. So please, bear with me. It's 2AM now, so I figure I've got about 4 hours to see if I can get to the bottom of this for you.




Theme © iAndrew 2016 - Forum software by © MyBB