lighttpd mod_redirect https to http specifically for media file types - redirect

I have a lighttpd 1.4.26 (ssl) configuration on a cent-os linux machine serving an HTML5 media application over HTTPS.
My goal is to serve media files via the application over HTTP from the same webserver.
If the webserver were located at https://www.media.com/ and all the media is located in various subfolders of http://www.media.com/sharedmedia/XXXXX, and I have relative links to any media file in the html for the pages served over http, then I want all requests to .mp3, .mp4, .webm, and .ogv files to be redirected to the EXACT SAME URL but using http instead of https...
My problem is I do not know how to write a url.redirect rule to perform this translation...
I have tried:
url.redirect = ( "https://^(.*)\.(ogv|mp4|mp3|webm)$" => "http://%1/$1" )
And when I visit this URL:
https://www.media.com/sharedmedia/X-MAC-MINI/Sports/Amazing%20Football%20Skills%20and%20Tricks.ogv
I am 301 permanently redirected to
http://www.media.com/sharedmedia/X-MAC-MINI/Sports/Amazing0Football0Skills0and0Tricks.ogv
Which is then also 301'ed to:
http:///sharedmedia/AFFINEGY-MAC-MINI/Sports/Amazing0Football0Skills0and0Tricks
Notice that the %20's that were in the very first url (urlencoded SPACE) were dropped from the URL leaving the trailing '0' in each case during the first redirect (I assume interpreted as %2 which holds an empty string), and that the http request is ALSO redirected erroniously to another URL that doesn't even contain the host value (www.media.com). Also, the extension is left off the second redirect...
I then tried a conditional version after that:
$HTTP["socket"] =~ ":443$"
{
url.redirect = ( "^(.*)\.(ogv|mp4|mp3|webm)$" => "http://%1/$1" )
}
..which results in lighttpd simply crashing on startup, so I can't even test it. Lighttpd startup error message follows:
Starting lighttpd: 2011-08-31 16:19:15: (configfile.c.907) source: find /etc/lighttpd/conf.d -maxdepth 1 -name '*.conf' -exec cat {} \; line: 44 pos: 1 parser failed somehow near here: (EOL)
2011-08-31 16:19:15: (configfile.c.907) source: /etc/lighttpd/lighttpd.conf line: 331 pos: 1 parser failed somehow near here: (EOL)
Any ideas what I'm doing wrong?

Here's what I did wrong:
You need a nested "IF" to use the %n notation in the destination IP rule...
I had
$HTTP["socket"] =~ ":443$"
{
url.redirect = ( "^(.*)\.(ogv|mp4|mp3|webm)$" => "http://%1/$1" )
}
But I needed
$HTTP["socket"] =~ ":443$" {
$HTTP["host"] == (.*) {
url.redirect = ( "^(.*)\.(ogv|mp4|mp3|webm)$" => "http://%1/$1.$2" )
}
}
Now... the %1 in "http://%1/$1" refers to the match in "$HTTP["host"] == (.*)" while $1 refers to the match in the url redirect parens, and $2 refers to the match for the second set of parens in the url redirect source...
Is it just me or is this shit TOTALLY undocumented? I can only find on google people complaining of not being able to get this to work and no one seems to have a single answer for how it works...
I'm now stuck making the EXACT same thing happen in APACHE, and I can't find a good documentation on .htaccess 301 redirects for it either...

Related

How to run Mojolicious appication under '/api' path( How to ignore some prefix in path )?

I have application it works fine. But now we decide to move it under /api path. So I use detour
my $r = $self->routes;
# Application is always under /api/v1, /api/v2 etc. path
$r = $r->any( '/api/:api', [ api => qr/v\d+/ ] )->detour( 'MyApp' );
$r->get( '/users/me' )->to( 'user#show_me' );
But after this nothing works. Request to site.domain/api/v1 cause application to fall into endless loop.
There is also Mojolicious::Plugin::Mount but it only useful to mount another one application under specified route.
This guide also does not resolve problem.
This line should be fixed:
# Application is always under /api/v1, /api/v2 etc. path
$r = $r->any( '/api/:api', [ api => qr/v\d+/ ] )->partial( 1 )
Update
As it was documented:
Route has no specific end, remaining characters will be captured in path.
If you run myapp.pl routes -v you will see:
/api/:api .D.. * apiapi ^\/api/((?^:v\d+))
+/users/me .... GET usersme ^\/users\/me/?(?:\.([^/]+))?$
When request come it will be checked against this regex:
^\/api/((?^:v\d+))\/users\/me/?(?:\.([^/]+))?$
Where only /users/me will be captured in the path

Nginx folder redirection without changing the url

I want to redirect an old path to a new one without changing the url and keeping the suffix of the url for the redirection:
dev-osm.blah.com/NYC/v2/site/links?key=a12345
redirect to
dev-osm.blah.com/NYC/v1/site/links2?key=a12345
server {
server_name dev-osm.blah.com
.
.
location ^~ /NYC/v2/site/links {
rewrite ^ /NYC/v1/site/links2
}
Use something like the following in your server config
location /NYC/v2
proxy_pass: /NYC/v1/

Nginx - How to redirect a directory to a file that its path contains the directory?

I'm new to Nginx and I'm trying to redirect a directory to a file, here is basically what I'm trying to do:
When entering this link:
http://localhost:8889/dir
go to this link instead:
http://localhost:8889/dir/path/to/the/file/index.html
As you can see the directory is part of the file path which I'm trying to redirect to. Plus there is already a location block in the config file to redirect that directory to the proper location:
location /dir {
root /opt/dir;
}
My first attempt was to use the rewrite directive as I saw from this blog(https://jeffsebring.com/2012/nginx-301-redirects/):
if ( $request_filename ~ dir ) {
rewrite ^ http://localhost:8889/dir/path/to/the/file/index.html permanent;
}
But the page says it has a redirect loop, which I believe is the conflict with the old location block.
Then I tried to add another location block as I saw from here(nginx rewrite virtual directory to file):
location /dir {
rewrite ^/dir$ /dir/path/to/the/file/index.html;
}
But after reloading config file, Nginx told me there is already a location block with the same directory exists.
So my question is is there any way I can do this? or it is not even possible?
Thanks!!
Found the solution.
I used "return" to return the full hard-coded url instead rewrite the current one:
location ~* /dir$ {
return http://localhost:8889/dir/path/to/the/file/index.html;
}
And also this rewrite solution works as well:
location ~* /dir$ {
rewrite ^/dir$ /dir/path/to/the/file/index.html;
}
The problem I had was actually caused by cache. I was actually reloading the page from cache so I could not see the result after changing nginx config file, and I resolved this by checking the "Disable cache" option in developer tools in Chrome.

PHP Built-in Webserver and Relative Paths

TL;DR
Does PHP 5.4 built-in webserver have any bug or restriction about relative paths? Or does it need to be properly (and additionally) configured?
When I used to programming actively I had a system working under URI routing using these lines in a .htaccess file:
RewriteEngine On
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php [L]
The FrontController received the Request, find the proper route from given URI in a SQLITE database and the Dispatcher call the the Action Controller.
It worked very nicely with Apache. Today, several months later I decided to run my Test Application with PHP 5.4 built-in webserver.
First thing I noticed, obviously, .htaccess don't work so I used code file instead:
<?php
if( preg_match( '/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"] ) ) {
return false;
}
include __DIR__ . '/index.php';
And started the webserver like this:
php.exe -c "php.ini" -S "localhost:8080" "path\to\testfolder\routing.php"
So far, so good. Everything my application need to bootstrap could be accomplished by modifying the include_path like this:
set_include_path(
'.' . PATH_SEPARATOR . realpath( '../common/next' )
);
Being next the core folder of all modules inside a folder for with everything common to all applications I have. And it doesn't need any further explanation for this purpose.
None of the AutoLoader techniques I've ever saw was able to autoload themselves, so the only class manually required is my Autoloader. But after running the Test Application I received an error because my AutoLoader could not be found. o.O
I always was very suspicious about realpath() so I decided to change it with the full and absolute path of this next directory and it worked. It shouldn't be needed to do as I did, but it worked.
My autoloader was loaded and successfully registered by spl_autoload_register(). For the reference, this is the autoloading function (only the Closure, of course):
function( $classname ) {
$classname = stream_resolve_include_path(
str_replace( '\\', DIRECTORY_SEPARATOR, $classname ) . '.php'
);
if( $classname !== FALSE ) {
include $classname;
}
};
However, resources located whithin index.php path, like the MVC classes, could not be found. So I did something else I also should not be doing and added the working directory to the include_path. And again, manually, without rely on realpath():
set_include_path(
'.' . PATH_SEPARATOR . 'path/to/common/next'
. PATH_SEPARATOR . 'path/to/htdocs/testfolder/'
);
And it worked again... Almost! >.<
The most of Applications I can create with this system works quite well with my Standard Router, based on SQLITE databases. And to make things even easier this Router looks for a predefined SQLITE file within the working directory.
Of course, I also provide a way to change this default entry just in case and because of this I check if this file exist and trigger an error if it doesn't.
And this is the specific error I'm seeing. The checking routine is like this:
if( ! file_exists( $this -> options -> dbPath ) ) {
throw RouterException::connectionFailure(
'Routes Database File %s doesn\'t exist in Data Directory',
array( $this -> options -> dbPath )
);
}
The dbPath entry, if not changed, uses a constant value Data/Routes.sqlite, relatively to working directory.
If, again, again, I set the absolute path manually, everything (really) works, the the Request flow reached the Action Controllers successfully.
What's going on?
This a bug in PHP's built-in web server that is still not fixed, as of PHP version 5.6.30.
In short, the web server does not redirect to www.foo.com/bar/ if www.foo./bar was requested and happens to be a directory. The client being server www.foo.com/bar, assumes it is a file (because of the missing slash at the end), so all subsequent relative links will be fetched relative to www.foo.com/instead of www.foo.com/bar/.
A bug ticket was opened back in 2013 but was mistakenly set to a status of "Not a Bug".
I'm experiencing a similar issue in 2017, so I left a comment on the bug ticket.
Edit : Just noticed that #jens-a-koch opened the ticket I linked to. I was not awar of his comment on the original question.

In Nginx, how do you configure a split redirect?

I want to configure Nginx so that this URL:
http://www.example.com/x/...
redirects here:
http://x.example.com/...
and everything else (not http://www.example.com/x/...)
http://www.example.com/...
remains as-is:
http://www.example.com/...
How do you configure Nginx to do this?
you do os by setting up 2 location blocks inside your server config: 1 with the redirect, and 1 with the default stuff that matches anything else like so:
location / {
# directives needed to serve the regular stuff
}
location /x/ {
rewrite ^/x/(.*)$ $1 last;
}