Main  /  Edit  /  History  /   /  Users Area

How to run the application at the root of the web server

Introduction

In most of the examples in this wiki the applications run from some basic URL - defined by the script name, for example "Hello World" application runs at "/hello" URL message board application runs from "/mb" URL.

It is simple and useful as all static files served as is and the application's URL use its own paths:

For example:

Static files:

/media/style.css

Application:

/mb
/mb/tree/1
/mb/comment/10 

Where "/tree/1" and "/comment/10" are application's sub path.

However we frequently may want to run the application in the "root" of the web service such that the main URL would be "/"

The naive approach - redirecting all requests to the application would not work as static files should be served from other location starting from "/" and it is not application's job to serve static files.

So to do this you need to use URL Rewriting rules that exist in any modern web server.

So if the application should start at the root "/" and all the media files should be served from "/media/" directory we would want to leave "/media/*" URL untouched and rewrite all other URLs like "/tree/1" to "/mb.fcgi/tree/1" where "/mb.fcgi" is out script.

So we would show how to create correct rewrite rules for the web server to serve the application in the host root.

We would assume that:

See How to configure the web server to run CppCMS applications tutorial.

Rewrite rules for different web servers

Apache

We should enable mod_rewrite and put in our <VirtualHost *> location following rewrite rules

RewriteEngine On
RewriteRule ^(/media/.*)$ $1 [PT]
RewriteRule ^(/favicon\.ico)$ $1 [PT]
RewriteRule ^/(.*)$ /mb.fcgi/$1 [QSA,L]

All the "/media/" files would be served as-is as files [PT] flag and the rest of the URL's like /some/url would be mapped into /mb.fcgi/some/url`

Lighttpd

We should enable mod_rewrite and provide following rule:

url.rewrite-once = (
  "^(/media/.*)"  => "$1",
  "^/favicon.ico$" => "$0",
  "^/(.*)" => "/mb.fcgi/$1"
)

Please note that we use rewrite-once directive such that once "^(/media/.*)" rule is executed it would not be rewritten any more.

since version 1.4.23 it's possibly to directly use

fastcgi.server = (
    "/"  => ((
        ## your configuration ... and after 
        "fix-root-scriptname" => "enable",
        "check-local" => "disable"
    ))
)

Nginx

The rewrite rule that should be defined before the FastCGI handler is following:

rewrite  ^(/media/.*)$     $1  last;
rewrite  ^(/favicon.ico)$  $1  last;
rewrite  ^/(.*)$  /mb.fcgi/$1 ;

Note: the last parameter is used for /media/.* root to prevent following substitution. This is not done fo the root URL ^/(.*)$ as FastCGI uses rewriting on its own.

CppCMS-Embedded

The http.rewrite section defines the URL rewriting rules. It is implemented in CppCMS sarting from CppCMS 0.99.11.

The http configuration section should look like:

"http" : {
  "script" : "/mb.fcgi",
  "rewrite" : [
    { "regex" : "/media(/.*)?", "pattern" : "$0" },
    { "regex" : "/favicon\\.ico", "pattern" : "$0" },
    { "regex" : ".*" , "pattern" : "/mb.fcgi$0" }
  ]
}

Useful Sources


Configure CppCMS to run with different web servers | Top | Customize your "Page not found | 404" page

About

CppCMS is a web development framework for performance demanding applications.

Support This Project

SourceForge.net Logo

Поддержать проект

CppCMS needs You


Navigation

Main Page



Valid CSS | Valid XHTML 1.0