Jump to content

Securely redirecting to another directory via mod_rewrite


HaLo2FrEeEk

Recommended Posts

If the title wasn't descriptive enough, basically what I want to do is set up a subdomain on my server that contains all the media (videos) that I have uploaded, then use a .htaccess file with a mod_rewrite command to redirect anyone that tries to access a certain file in the existing folder to that file in the new subdomain.

 

For example, if I have a video in /movies under my main domain directory, and the file is called somevideo.wmv, a person would currently access this file by going to http://mydomain.com/movies/somevideo.wmv.  Well if I make a new subdomain, something like media.mydomain.com and put the videos in there, then when someone went to that URL above the file wouldn't be there anymore.  I want to make a rewrite rule to make it so that if someone goes to http://mydomain.com/movies/somevideo.wmv it will redirect silently to media.mydomain.com/somevideo.wmv, is this as simple as I think it will be, an .htaccess file inside the /movies directory with this:

 

RewriteRule (.*)\.[wmv|mpg|mpeg|mov|mp4] http://media.mydomain.com/movies/$1\.$2 [NR]

 

Or something like that?  I'm not that great at mod_rewrite and I wrote that all from scratch, so I'm sure it has errors, if someone could help me fix them.  My main issue with that is what if someone puts in something like ../../ as the filename and tries to access files they shouldn't, in parent folders or something?  Could they put something like http://mydomain.com/movies/../mydomain.com/index.php and download the index file from my main domain's directory?  I don't want to try something like this in case it actually does that...but I might have to.

 

Please, though, there's only so much I can test myself, not knowing that much about mod_rewrite, so can I get some help from the experts.

 

Thanks in advance!

I went to anything.mydomain.com and it gave me a 404 error.  I don't think my server can do wildcard subdomains, but I'm not sure that would work for what I'm trying to do anyway.

 

I figured it out though:

 

RewriteRule (.*)\.(exe|scr|mp4|wmv|mov) http://media.mydomain.com/$1\.$2

 

will redirect properly, so all I have to do is move all the files from /movies into the new media subdomain, then put this in the movies folder (which I'm gonna keep for other things) in my main domain.  I've tested it and it works great!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.