3

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.

3

You are very close to what you like to have, you only missing ( & )

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]

The point is %1 refers to first Capturing Group in your Rewrite Conditions. Capturing Group are between ( & ) and in your case this was the URI Extension (mp4|mp3|mov|mpg|mpeg|webm|wmv|ogg). Adding the a new Capturing Group in you Host Condition give you two Capturing Groups, but for your Rewrite Rule you only need the first one.

One Additional Remark: This only works for the main Domain with no other sub domain than www if you, have a FQDN like www.video.example.com it will not work.

EDIT:

The first Code Example is basically focusing only on the main Issue, but as mentioned in the commands, the hole Rewrite rule could be optimized.

RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.com$ [NC]
RewriteRule ^/?(.*\.(mp4|mp3|mov|mpg|mpeg|webm|wmv|ogg))$ https://www.webvideo.com/%1_video/$1 [R=301,NC,L,NE]
  • 4
    You'll also need to reverse the two RewriteCond directives. The %1 backreference matches the first captured group in the last matched CondPattern. So, in this example, %1 would still refer to the file extension, not the domain name, since that is the last matched CondPattern. However, it would be preferable to remove the RewriteCond directive that checks the file extension altogether and incorporate this check in the RewriteRule pattern instead. – MrWhite Oct 6 '17 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.