lighttpd url rewrite to subdomain - regex

How does the lighttpd rewrite work for folowing?
http://example.com/file_46634643.jpg to http://sub.domain.com/46634643.jpg
If it's possible...

Yes, it is possible. Use mod_rewrite, here is an example:
url.rewrite-once = ("^/file_([0-9]+\.jpg)$" => "/$1")
Check Lighttpd's mod_rewrite documentation for more details.

I don't think mod_rewrite allows you to "rewrite" to another (sub)domain. You could use mod_redirect (but it's a redirect, not a rewrite). Here is a slightly modified version of Bartosz's answer:
$HTTP["host"] == "example.com" {
url.redirect = ( "^/file_([0-9]+\.jpg)$" => "http://sub.domain.com/$1" )
}
Check http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModRedirect

Related

WordPress PHP Redirect with Regex Wildcard

This is for a project. I need to redirect certain incoming URL requests to files hosted on the same webserver. I cannot use .htaccess and I cannot rely on any plugins.
I managed to do this with a plugin but I am having a hard time extracting the necessary code in order to write my own plugin which does what I need to do hardcoded.
WordPress Multisite
All software up to date
"Redirection" WordPress Plugin (sucessfully)
writing custom WP functions to do this (semi-successfully)
I found some code in the Pantheon documentation:
$blog_id = get_current_blog_id();
// You can easily put a list of many 301 url redirects in this format
// Trailing slashes matters here so /old-url1 is different from /old-url1/
$redirect_targets = array(
'/test/test.xml' => '/files/' . $blog_id . '/test.xml',
'/regex/wildcard.xml(.*)' => '/files/' . $blog_id . '/regex.xml',
);
if ( (isset($redirect_targets[ $_SERVER['REQUEST_URI'] ] ) ) && (php_sapi_name() != "cli") ) {
echo 'https://'. $_SERVER['HTTP_HOST'] . $redirect_targets[ $_SERVER['REQUEST_URI'] ];
header('HTTP/1.0 301 Moved Permanently');
header('Location: https://'. $_SERVER['HTTP_HOST'] . $redirect_targets[ $_SERVER['REQUEST_URI'] ]);
if (extension_loaded('newrelic')) {
newrelic_name_transaction("redirect");
}
exit();
}
https://example.com/test/test.xml sucessfully
redirects to
/files/5/text.xml
However, some incoming requests contain a query string, e.g.
https://example.com/regex/wildcard.xml?somequerystring
obviously, the redirect from
https://example.com/regex/wildcard.xml
to
/files/5/regex.xml
works fine.
However, as soon as there is a query string involved, the redirect does not work. Given that I need to do this with PHP, how can I achieve a wildcard redirect from either /regex/* or /regex/wildcard.xml* to /files/5/regex.xml?
Any help would be greatly appeciated.
Thanks,
Daniel
If you prefer to retain your code logic and just make the code work, then try this:
$blog_id = get_current_blog_id();
$redirect_targets = array(
'/wordpress\/test\/test\.xml/i' => 'files/'.$blog_id.'/test.xml',
'/([^\/]+)\/([^\/]+)\.xml.*/i' => 'files/'.$blog_id.'/$1.xml',
);
// Get reuest uri without GET attributes.
$request_uri = get_request_uri();
// Loop through redirect rules.
foreach ($redirect_targets as $pattern => $redirect) {
// If matched a rule, then create a new redirect URL
if ( preg_match( $pattern, $request_uri ) ) {
$new_request_uri = preg_replace( $pattern, $redirect, $request_uri );
$new_url = 'https://'.$_SERVER['HTTP_HOST'].$new_request_uri;
header( 'HTTP/1.0 301 Moved Permanently' );
header( 'Location: '.$new_url );
if ( extension_loaded( 'newrelic' ) ) {
newrelic_name_transaction( "redirect" );
}
exit();
}
}
// Returns REQUEST URI without 'get' arguments
// if example.com/test/test.php?some=arg it will return test/test.php
function get_request_uri () {
return strtok( $_SERVER['REQUEST_URI'], '?' );
}
You can modify redirect rule as you want. It works as a normal Regex pattern.

What is the best way to handle Nginx rewrite with subdomains?

I need to parse out a subdomain and add it to end of the url, for example:
subdomain.mysite.com needs to rewrite to subdomain.mysite.com/subdomain
subdomain.mysite.com/login needs to rewrite to subdomain.mysite.com/subdomain/login
Trouble arises because I have a set of reserve words that I don't want to match, for example the various different environments.
dev-web.mywebsite.com should not map to mywebsite.com/dev-web
This is what I have so far... am struggling with the nginx syntax and regex in general.
if ($host ~ ^([^.]+)\.(.+)) {
set $subdomain $1;
}
if ($subdomain ~* ^(dev-web|uat-web)$) {
rewrite ^ $scheme://$host/$subdomain$request_uri permanent;
}
The various errors I get are too many redirects or it is just not redirecting at all.
ERR_TOO_MANY_REDIRECTS
subdomain.mysite.com/subdomain/subdomain/subdomain/subdomain...

How to find regex in referer url?

To deny some spammers who abuse valid referes to attack certain urls, I'd need a regexp rule to deny all google.* referes to /some/target/url
Here is what I've tried:
location ^~ /some/target/url {
if ($http_referer ~ (.*)google(.*))
{ return 400;
}
}
UPDATE:
What I want to avoid in the rule above are all google.* referers, e.g.
google.com
google.de
google.co.uk
google.jp
...
But, based on access.log, the above directive (and others that I've tried) do not work. So I appreciate your help.
No need for the parenthesis, so you can have:
~ google
to be case-insensitive:
~* google
Then you'll probably want to make it more robust, e.g., to avoid http://www.your.com/google.

Manipulate reg expression variables in .htaccess

I want to redirect the client from
http://mydomaine/cat?start=x
to
http://mydomaine/cat/page-(x divide by 15)
e.g from http://mydomaine/cat?start=30
to
http://mydomaine/cat/page-2
There any way to do that with .htaccess?
Not sure you can do this in .htacces file.
But, if you are under Joomla (from the tag of your question) you can create a system plugin which will let you manipulate urls.
Here is the doc from joomla.org Documentation
I think this tip only work with joomla 2.5.
Thanks all of you, so this the solution that is working for me:
if (preg_match("/(.*)\?limitstart=[0-9]+/", JRequest::getURI(), $matches)) {
header('Status: 301 Moved Permanently', false, 301);
header("Location: ".$matches[1]);
exit();
}

Set different Lighttpd vhost for internal LAN clients - possibly just RegEx required...?

I want Lighttpd to display a different page for internal clients and the default page for everyone else.
Between these two links, I have an idea of what I want to do but am not sure of the RegEx I would need to restrict clients using a hostname of [http://]192.168.0.? or [http://]192.168.?.? to a different page. I've been using the following code in lighttpd.conf:
server.document-root = "/var/www/sites"
$HTTP["host"] == "RegExHere" {
server.document-root = "/var/www/setup"
}
...where for 'RegExHere' I have tried a variety of attempts such as:
192\.168\.0\.\d{1,3}(\s|$))+
192\.168\.
[192.168.[0-9]+.]
192\.168\.[0-9]+.[0-9]+$
...and various combinations thereof. I have no idea whether I'm close, but regardless it only shows me the default page.
Can anyone advise where I may be going wrong please?
Thanks in advance!
You have to use the =~ syntax to match a regex. Change $HTTP["host"] == "RegExHere" to $HTTP["host"] =~ "RegExHere" and one of those regexs should work. ^192\.168\.\d{1,3}\.\d{1,3}$ should do it.
Found this article on it http://blog.evanweaver.com/2006/06/07/regular-expressions-in-lighttpd-host-redirects/
edit: I think you need to use $HTTP["remoteip"] instead of $HTTP["host"] and it looks like you can do it without regexes.
$HTTP["remoteip"] == "10.0.0.0/8" { url.access-deny = ("") }
$HTTP["remoteip"] == "127.0.0.0/8" { url.access-deny = ("") }
http://forum.lighttpd.net/topic/27