Author Topic: need some help with my mod rewrite  (Read 2081 times)

micah

  • A real person, on the Internet.
  • Ass Wipe
  • Posts: 6915
  • Karma: +58/-55
  • Truth cannot contradict truth.
    • micahj.com
need some help with my mod rewrite
« on: October 15, 2010, 10:34:06 AM »
EDIT:  I THINK I FIXED MY PROBLEM.  SO YOU CAN IGNORE THIS THREAD NOW

sorry, this is long, but its otherwise hard to explain...

So i've built this fairly complex template website system that lets several different affiliates of an organization set up their own website off of one templated site.  Basically, all the individual domain names point to the one site and then, the site determines which content to load based on the domain name.  For most of the site I'm just using PHP's $_SERVER array to figure out the domain name and this works great.

I've also added some mod rewrite rules in an .htaccess file to mask where each site's files are stored.  Basically, each affiliate gets their own folder for images, PDF's and other downloadable content.  These folders are all aliased so that, looking at the html code, you can't tell the site is part of the template system.

In other words, the folder  /sites/some-affiliates-website.com/images/  looks like /media/images.

the code to do this looks like this:
Code: [Select]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC,OR]
RewriteCond %{HTTP_HOST} ^(.+)$ [NC]
RewriteRule ^media(.*)$ /sites/%1/$1 [QSA,L]

this works great.

Here's where it gets tricky...

Since some affiliates that are joining this system already have a site with their domain name pointing to it - or they may not have procured their domain name yet, I've had to set up a way to allow them to build/view their website without looking for a domain name in the url.

I've created a subdomain off the main site (http://staging.rebuildingtogetheraffiliates.org) which then lets the user enter their (eventual) domain name and it sets a cookie that the PHP script uses to load content (instead of a domain name in the url)

and this works great as well.  Of course, that only solves the PHP side of things.  the /media/ alias doesn't work so I've added a line to the htaccess script:
Code: [Select]
RewriteCond %{HTTP_COOKIE} stagingdomain=(.*) [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC,OR]
RewriteCond %{HTTP_HOST} ^(.+)$ [NC]
RewriteRule ^media(.*)$ /sites/%1/$1 [QSA,L]

So now, htaccess is looking for the cookie first, if its there, it add the alias. if it not, it works the way it did before.

this works too! 

But here's the bug.  SOMETIMES when I go to an affiliates site and don't add the "www" in the url, the images in the /media/ alias don't show up.  But sometimes they do.

The demo site I've built for test uses the .com version of the system's domain name:

http://www.rebuildingtogetheraffiliates.com  always works

http://rebuildingtogetheraffiliates.com  sometimes works

http://staging.rebuildingtogetheraffiliates.org  (then enter "rebuildingtogetheraffiliates.com")  - always works

So here's were I'm looking for some help/advice or whatever.

1) when you go to the site with and without the "www" do all the images always appear?
2) what if you log into the staging site and then go back to the .com site?  (the cookie is for the .org domain so I don't know why it would matter, but who knows?)
3) looking at my htaccess code, what can I change to fix this problem?  I was thinking I could just force the "www" but I can't always force it because sometimes in needs to be "staging."
« Last Edit: October 15, 2010, 11:58:49 AM by micah »
"I possess a device, in my pocket, that is capable of accessing the entirety of information known to man.  I use it to look at pictures of cats and get in arguments with strangers."

micah

  • A real person, on the Internet.
  • Ass Wipe
  • Posts: 6915
  • Karma: +58/-55
  • Truth cannot contradict truth.
    • micahj.com
Re: need some help with my mod rewrite
« Reply #1 on: October 15, 2010, 12:00:30 PM »
I think i've resolved this.  its not an issue in the htaccess file.   there was a php coding error on my part that was adding the stagingdomain cookie even when not using the staging subdomain.  when/if this value got corrupted, it was breaking the /media/ alias.  I think this is fixed now. 

I love the timing on these things.  this bug has been bothering me for weeks-- then i spend 20 minutes writting it all out in a big long post and an hour later I fix it on my own.  go figure
 :rolleyes:
"I possess a device, in my pocket, that is capable of accessing the entirety of information known to man.  I use it to look at pictures of cats and get in arguments with strangers."

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14310
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: need some help with my mod rewrite
« Reply #2 on: October 15, 2010, 02:15:50 PM »
Shit like that happens to me all the time.

Mike

  • Jackass In Charge
  • Posts: 11257
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: need some help with my mod rewrite
« Reply #3 on: October 15, 2010, 02:18:55 PM »
I love the timing on these things.  this bug has been bothering me for weeks-- then i spend 20 minutes writting it all out in a big long post and an hour later I fix it on my own.  go figure
 :rolleyes:
I can't even count the number of times I've typed up a big long post asking for help and then figure out the problem before hitting submit.  I think sometimes writing it out helps.  I try to detail everything I did, what the results were, etc and that seems to point out things I hadn't tried yet.