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!

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.