Server configurations for using friendly URLs

Server configurations for using friendly URLs configurations for using friendly URLs Getting Started

Let’s look at the basic server configurations: Apache, Nginx and Lighttpd, which must be in order for friendly URLs (CHPU) to work in MODX.

Working example of .htaccess for Apache servers

Apache HTTP server is currently the most popular, it is used by at least 50% of all hostings in the world, very often it comes in conjunction with Nginx.

MODX provides a ht.access file for editing in accordance with your server settings. This file appears after installing the CMS. It is located in the root of the MODX site. This file will be ignored by the server until you rename it to .htaccess.

For most hosts, you don’t need to make any changes to the file (renamed and that’s it) to make friendly URLs work.

Here are some example lines to make Friendly URLs work:

RewriteEngine On
RewriteBase /

# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

On some specific hostings (there are few of them), you need to move the .htaccess file from the site root one level higher: to /htdocs or /public_html for it to work.

Also, many hosters automatically add their rules to the .htaccess file, therefore the rules described above should be either above the rules entered by the hoster, or vice versa, below them. Before any edits, I recommend making a copy of the host file.

Nginx Server Config

Here is an example of nginx server configuration for installing MODX (php-fpm is required). This example allows you to use friendly MODX URLs.

server {
        listen 80;
        server_name example.com www.example.com;
        root /home/sites/example.com;
        index index.php;
        client_max_body_size 30M;
        location / {
                root /home/sites/example.com;
                if (!-e $request_filename) {
                        rewrite ^/(.*)$ /index.php?q=$1 last;
                }
        }
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(.*)$;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include fastcgi_params;
                fastcgi_ignore_client_abort on;
                fastcgi_param  SERVER_NAME $http_host;
        }

        location ~ /\.ht {
                deny  all;
        }
}

The FastCGI connection between nginx and PHP, expressed in the line fastcgi_pass 127.0.0.1:9000; should be set to something like fastcgi_pass unix:/var/run/php5-fpm.sock;

This depends on how the www.conf file is configured (usually located in /etc/php5/fpm/pool.d ). How to configure the “listen” directive in this file: TCP or unix socket (such as /var/run/php5-fpm.sock)?

The nginx config file must specify the same connections in both files! In theory, unix sockets will be faster, but in this case both resources must be on the same host. TCP is useful in a distributed environment.

Here is another example of an alternative configuration:

server {
	listen 80;
	server_name www.financetrails.com financetrails.com;
	root /path/to/www.financetrails.com;
	index index.php index.html;

	location / {
		#try to get file directly, try it as a directory or fall back to modx
		try_files $uri $uri/ @modx;
	}

	location @modx {
		#including ? in second rewrite argument causes nginx to drop GET params, so append them again
		rewrite ^/(.*)$ /index.php?q=$1&$args;
	}

	location ~ \.php(.*)$ {
		include fastcgi_params;
		fastcgi_pass backend;
		fastcgi_index index.php;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
	}
}

Also make sure that your php.ini contains the line: cgi.fix_pathinfo=0, otherwise you may be subject to attacks via uploaded php files.

Guide to configuring and friendly URLs in Lighttpd

Below is described only the configuration of URL rewriting and the operation of friendly URLs (for an already working lighttpd+mysql+PHP bundle).

lighttpd uses a different URL rewriting system than Apache. URL rewriting is done in the lighttpd.conf file

  • First, you need to make sure that the URL rewriting module is enabled.
    • Open your lighttpd.conf configuration file (On Linux systems, it is usually located /etc/lighttpd/lighttpd.conf).
    • Find the server.modules directive.
    • Find the mod_rewrite entry in this directive.
    • By default, it has a # in front of it. This is a comment symbol. Remove the # from the line and save the file.
  • Next, we need to find a place to put the URL friendly code. Find code similar to this:
$SERVER["socket"] == ":80" {
$HTTP["host"] =~ "yourdomainname.com" {
    server.document-root = "/path/to/your/doc/root"
    server.name = "yourservername"
  • Directly below this you should add the following code.
url.rewrite-once = ( "^/(assets|manager|core|connectors)(.*)$" => "/$1/$2",
    "^/(?!index(?:-ajax)?\.php)(.*)\?(.*)$" => "/index.php?q=$1&$2",
    "^/(?!index(?:-ajax)?\.php)(.*)$" => "/index.php?q=$1"
)

Note! Lighttpd handles URLs a little differently. In the configuration file, you must exclude any files and/or folders that you don’t want to rewrite. In the example above, the directories/files (assets | manager | core | connectors) are excluded. If you want to add more to the exclusions, just add another | and then specify the file/folder name that you want to skip when rewriting URLs.

Now friendly URLs will work.

Rate article
MODX 3
Add a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.