example.com/channels?watch=channel_name&v=videoId
It's still unclear exactly what you are trying to do here since you've not identified the script (ie. front-controller) that would actually handle this request. What is the name of the script that reads the URL parameters and ultimately serves the content?
You've stated that /channels
is a physical directory on the filesystem (so the URL-path would need to at least end in a slash, ie. /channels/?watch=channel_name&v=videoId
) so this isn't strictly a valid end-point for a rewrite (as it still requires additional rewriting to script that would actually do something). It would need to be of the form (assuming PHP):
example.com/channels/front-controller.php?watch=channel_name&v=videoId
Where front-controller.php
is the script that actually handles the request (ie. the front-controller).
In order to achieve this rewrite (from /channels/channel_name?v=videoId
) you could do something like the following in the root .htaccess
file:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(v=videoId)$
RewriteRule ^channels/([\w]+)$ channels/front-controller.php?watch=$1&%1 [QSA,L]
Where the channel_name
can consist of the characters a-z
, A-Z
, 0-9
and _
(which covers your example URL).
/channels/front-controller.php
is the script that actually handles the request and returns the content. Although, this can be anywhere, it doesn't need to be in the /channels
directory (in fact, it would be easier if it wasn't - to avoid potential conflicts).
%1
is a backreference to the captured group in the preceding CondPattern. ie. the value v=videoId
. Saves repetition.
This is an internal rewrite - the URL that the user sees stays as /channels/channel_name?v=videoId
.
I have a workaround solution:
RewriteRule ^channels/(.*)$ /watch/?channel=$1 [R=302,NC,QSA,L]
Although this is an external redirect - the URL the user sees is modified to the new URL. What file handles the request in this case? Is the v=videoId
parameter not required?