大网数据广告活动
服务器运维问答

Apache rewrite to extract multiple pieces of data from original request URL

· 52CE 我爱测

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_H…

问题原文

原帖链接 · K.McCallum · CC BY-SA 3.0

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.

已采纳解答

原帖链接 · Webdesigner · CC BY-SA 3.0

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]

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-10-06)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。