Lighttpd vhost setup - virtualhost

tl;dr - How do I reference the conditional regex matches?
I am looking for the simplest vhost setup, but what I am trying doesn't work.
I want:
http://example.dev` => /var/www/dev/example/
http://website.dev` => /var/www/dev/website/
I have tried:
server.document-root = "/var/www/"
$HTTP["host"] =~ "^(.+)\.(.+)$" {
server.document-root += "%2/%1/"
}
What my method resolves to:
Path: /var/www/%2/%1

I suspect that the %1 %2 syntax only works with mod_rewrite. I can't confirm that, but I've only ever used it with mod_rewrite.
A mod_rewrite solution would be the following:
server.document-root = "/var/www/"
$HTTP["host"] =~ "^(.+)\.(.+)$" {
url.rewrite-once = ( "(.*)" => "/%2/%1$1" )
}
Which should effectively act as if your document root has moved.
*This is untested

Related

Redirect, Hide Folder Name and enabling URL access with multiple subdirectory [duplicate]

I have a URL that looks like:
url.com/picture.php?id=51
How would I go about converting that URL to:
picture.php/Some-text-goes-here/51
I think WordPress does the same.
How do I go about making friendly URLs in PHP?
You can essentially do this 2 ways:
The .htaccess route with mod_rewrite
Add a file called .htaccess in your root folder, and add something like this:
RewriteEngine on
RewriteRule ^/?Some-text-goes-here/([0-9]+)$ /picture.php?id=$1
This will tell Apache to enable mod_rewrite for this folder, and if it gets asked a URL matching the regular expression it rewrites it internally to what you want, without the end user seeing it. Easy, but inflexible, so if you need more power:
The PHP route
Put the following in your .htaccess instead: (note the leading slash)
FallbackResource /index.php
This will tell it to run your index.php for all files it cannot normally find in your site. In there you can then for example:
$path = ltrim($_SERVER['REQUEST_URI'], '/'); // Trim leading slash(es)
$elements = explode('/', $path); // Split path on slashes
if(empty($elements[0])) { // No path elements means home
ShowHomepage();
} else switch(array_shift($elements)) // Pop off first item and switch
{
case 'Some-text-goes-here':
ShowPicture($elements); // passes rest of parameters to internal function
break;
case 'more':
...
default:
header('HTTP/1.1 404 Not Found');
Show404Error();
}
This is how big sites and CMS-systems do it, because it allows far more flexibility in parsing URLs, config and database dependent URLs etc. For sporadic usage the hardcoded rewrite rules in .htaccess will do fine though.
If you only want to change the route for picture.php then adding rewrite rule in .htaccess will serve your needs, but, if you want the URL rewriting as in Wordpress then PHP is the way. Here is simple example to begin with.
Folder structure
There are two files that are needed in the root folder, .htaccess and index.php, and it would be good to place the rest of the .php files in separate folder, like inc/.
root/
inc/
.htaccess
index.php
.htaccess
RewriteEngine On
RewriteRule ^inc/.*$ index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
This file has four directives:
RewriteEngine - enable the rewriting engine
RewriteRule - deny access to all files in inc/ folder, redirect any call to that folder to index.php
RewriteCond - allow direct access to all other files ( like images, css or scripts )
RewriteRule - redirect anything else to index.php
index.php
Because everything is now redirected to index.php, there will be determined if the url is correct, all parameters are present, and if the type of parameters are correct.
To test the url we need to have a set of rules, and the best tool for that is a regular expression. By using regular expressions we will kill two flies with one blow. Url, to pass this test must have all the required parameters that are tested on allowed characters. Here are some examples of rules.
$rules = array(
'picture' => "/picture/(?'text'[^/]+)/(?'id'\d+)", // '/picture/some-text/51'
'album' => "/album/(?'album'[\w\-]+)", // '/album/album-slug'
'category' => "/category/(?'category'[\w\-]+)", // '/category/category-slug'
'page' => "/page/(?'page'about|contact)", // '/page/about', '/page/contact'
'post' => "/(?'post'[\w\-]+)", // '/post-slug'
'home' => "/" // '/'
);
Next is to prepare the request uri.
$uri = rtrim( dirname($_SERVER["SCRIPT_NAME"]), '/' );
$uri = '/' . trim( str_replace( $uri, '', $_SERVER['REQUEST_URI'] ), '/' );
$uri = urldecode( $uri );
Now that we have the request uri, the final step is to test uri on regular expression rules.
foreach ( $rules as $action => $rule ) {
if ( preg_match( '~^'.$rule.'$~i', $uri, $params ) ) {
/* now you know the action and parameters so you can
* include appropriate template file ( or proceed in some other way )
*/
}
}
Successful match will, since we use named subpatterns in regex, fill the $params array almost the same as PHP fills the $_GET array. However, when using a dynamic url, $_GET array is populated without any checks of the parameters.
/picture/some+text/51
Array
(
[0] => /picture/some text/51
[text] => some text
[1] => some text
[id] => 51
[2] => 51
)
picture.php?text=some+text&id=51
Array
(
[text] => some text
[id] => 51
)
These few lines of code and a basic knowing of regular expressions is enough to start building a solid routing system.
Complete source
define( 'INCLUDE_DIR', dirname( __FILE__ ) . '/inc/' );
$rules = array(
'picture' => "/picture/(?'text'[^/]+)/(?'id'\d+)", // '/picture/some-text/51'
'album' => "/album/(?'album'[\w\-]+)", // '/album/album-slug'
'category' => "/category/(?'category'[\w\-]+)", // '/category/category-slug'
'page' => "/page/(?'page'about|contact)", // '/page/about', '/page/contact'
'post' => "/(?'post'[\w\-]+)", // '/post-slug'
'home' => "/" // '/'
);
$uri = rtrim( dirname($_SERVER["SCRIPT_NAME"]), '/' );
$uri = '/' . trim( str_replace( $uri, '', $_SERVER['REQUEST_URI'] ), '/' );
$uri = urldecode( $uri );
foreach ( $rules as $action => $rule ) {
if ( preg_match( '~^'.$rule.'$~i', $uri, $params ) ) {
/* now you know the action and parameters so you can
* include appropriate template file ( or proceed in some other way )
*/
include( INCLUDE_DIR . $action . '.php' );
// exit to avoid the 404 message
exit();
}
}
// nothing is found so handle the 404 error
include( INCLUDE_DIR . '404.php' );
this is an .htaccess file that forward almost all to index.php
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !-l
RewriteCond %{REQUEST_FILENAME} !\.(ico|css|png|jpg|gif|js)$ [NC]
# otherwise forward it to index.php
RewriteRule . index.php
then is up to you parse $_SERVER["REQUEST_URI"] and route to picture.php or whatever
PHP is not what you are looking for, check out mod_rewrite
Although already answered, and author's intent is to create a front controller type app but I am posting literal rule for problem asked. if someone having the problem for same.
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([\d]+)$ $1?id=$3 [L]
Above should work for url picture.php/Some-text-goes-here/51. without using a index.php as a redirect app.

Lighttpd reverse proxy

I have a reverse proxy setting in my Apache's httpd.conf:
ProxyPass "/endpoint" "https://someurl.com/endpoint"
ProxyPassReverse "/endpoint" "https://someurl.com/endpoint"
And I need to replicate this in Lighttpd. I'm running a JS app which calls localhost:8080/endpoint to retrieve some data. I'd like to set up a proxy to always redirect /endpoint to https://someurl.com/endpoint.
In my lighttpd.conf I have the following settings:
server.modules = ("mod_proxy")
$HTTP["url"] =~ "^.*endpoint" {
proxy.server = ( "" => (( "host" => "https://someurl.com/endpoint" ) ) )
}
based on this SO answer.
I have also tried:
server.modules = ("mod_proxy")
proxy.server = ( "/endpoint" => (( "host" => "https://someurl.com/endpoint" )))
based on the lighttpd docs.
In both cases, I'm still hitting localhost:8080/endpoint which results in a 404 error. How do I set up the proxy correctly?
In lighttpd 1.4.46 and later, you can use proxy.header. See
https://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_ModProxy
server.modules = ("mod_proxy")
$HTTP["url"] == "/endpoint" {
proxy.server = ( "" => (( "host" => "someurl.com" )))
proxy.header = ( "map-host-request" => ( "-" => "someurl.com"),
"map-host-response" => ("-" => "-"))
}

OpenWRT HTTPS/SSL Traffic Redirect

I have the following problem:
I'm running a router with openwrt and a lighttpd webserver and i'm trying to redirect https traffic to a specific domain.
Here is my lighttpd.conf:
$SERVER["socket"] == ":443" {
url.redirect = (
"" => "http://name.tld",
)
}
If I call routerip:443 everything works fine,
but when I call https://routerip it gives me an error, for example:
ERR_NETWORK_CHANGED
or something with DNS_ERROR
I suspect it is relying explicitly on the redirect destination, which in your example still uses "http" as the protocol. Try modifying your redirect to include https:
url.redirect = (
"" => "https://name.tld",
)

Nginx: how to do rewrite?

My website have three different pages:
www.abc.com/
www.abc.com/node
www.abc.com/memeber
If I type www.abc.com in the browser,it goes to www.abc.com/, how can I change it to www.abc.com/node when I type www.abc.com and keep the www.abc.com/memeber normal as usual?
Just adding a small note, use return instead of rewrite for redirects, check nginx pitfalls
location = / {
return 301 $scheme://$server_name/node
}
location / {
# normal location handling, using try_files for example
}
Following worked for me provided I was only serving static files.
server {
listen 80;
server_name www.abc.com;
root /var/www/abc;
if ($request_uri = '/') {
rewrite ^ /node break;
}
}
location = / {
rewrite ^ /node;
}

Redirect full website address to another address - Apache

How can I redirect the following via a virtual host:
http://www.surveys.abc.com/index.php?sid=14414&newtest=Y&lang=en
to
http://www.abc.com
You don't really need to use Apache's mod_rewrite for that.
You can tweak your PHP code in index.php to look for those appropriate values in $_GET and then do the PHP redirect like this:
if (isset($_GET['sid']) && ($_GET['sid']==14414) && isset($_GET['newtest']) && ($_GET['newtest']==Y) && isset($_GET['lang']) && ($_GET['lang']=="en") ) {
header( 'Location: http://www.abc.com' );
}