Trying to create an Apache rewrite that applies to video files only, over to a dedicated video-serving web-server.
It requires rewrite logic that extracts multiple pieces of information from the original request, including HTTP_HOST
domain name, request_uri, and target file type. I've done a great bit of searching for assistance and suggestions, and gone through a tremendous amount of trial-&-error...... and have only gotten close, and can't seem to get it 100% correct. Here's what I'm trying to accomplish:
Example incoming URL:
https://www.example1.com/images/source/vidtest.mp4
Intended landing URL:
https://www.webvideo.com/example1_video/images/source/vidtest.mp4
So a redirect that applies ONLY to requests that end in video file-extension types (such as .mp4
), that extracts the incoming domain name only (not the leading www
, or the trailing .com
from the original HTTP_HOST), and extracts the remaining URI.... and redirects it to a new web-server, into a video folder that's named with that original host domain name (eg. example1
) followed by _video
, and then the remainder of the entire original URI appended.
Here is the redirect logic I currently have:
RewriteCond %{HTTP_HOST} ^www\.[^.]+\.com$
RewriteCond %{REQUEST_URI} \.(mp4|mp3|mov|mpg|mpeg|webm|wmv|ogg)$ [NC]
RewriteRule ^/(.*)$ https://www.webvideo.com/%1_video/$1 [R=301,NC,L,NE]
It seems to be working correctly to pick up only incoming requests for one of those video file-extension types, and it's properly redirecting to the intended target www.webvideo.com
domain, and it's also properly appending the trailing URI as well (the $1
variable). But unfortunately, it's putting the incoming file-type (such as mp4
) in the %1 variable location prior to _video
, and not the original domain name. So that example incoming request results in getting redirected to this:
https://www.webvideo.com/mp4_video/images/source/vidtest.mp4
Can someone suggest a correction? What do I have to do to get that original domain name out as a variable and into the new folder path, AND have the redirect only apply to video file types, AND carry over the rest of the trailing URI as well? Note that there will be incoming requests for multiple different domains (which is why the domain name needs to be extracted for the new folder path!) that will all be redirected to the same single web-serving apache target.