Docs

Rewrites without .htaccess / Short URL

Short URL is good for webpage visitors. You can achieve short URL using rewrites.

nginx has got no .htaccess, where you could set up so popular mod_rewrite.

Never the less, nginx has own module ngx_http_rewrite_module to rewrite URL. Rewrites in nginx are more flexible and comprehensible than Apache's mod_rewrite.

Rewrite rules should be written in an individual website config.

You can convert .htaccess to nginx configuration using our online htaccess-to-nginx converter.

Rewrite rule example

rewrite  ^/users/(.*)$  /show.php?user=$1  last;

If the server got a request /users/alex, then it will rewrite this URL to /show.php?user=alex as a result a PHP script /show.php will be run with a GET param user. At the same time, the visitor will see that short URL in his browser.

Example for Drupal

<FilesMatch "\.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$">
    Order allow,deny
</FilesMatch>

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]

Configure nginx:

location ~* \.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$ {
    deny all;
}

location ~* ^/(\..*|Entries.*|Repository|Root|Tag|Template)$ {
    deny all;
}

location / {
    try_files $uri /index.php$is_args$args;

    ## other way:
    #if (!-f $request_filename) {
    #   rewrite ^(.*)$ /index.php;
    #}
}

location /favicon.ico {
}

Example for Joomla

RewriteBase /
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]

Configure nginx:

location / {
    try_files $uri /index.php$is_args$args;
}

Example for Wordpress

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Configure nginx:

location / {
    try_files $uri /index.php$is_args$args;
}

Example for MODX

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_USER_AGENT} ^.*internal\ dummy\ connection.*$ [NC]
RewriteRule .* - [F,L]

RewriteRule ^(manager|assets) - [L]

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

Configure nginx:

if ($http_user_agent ~* "internal dummy connection") {
    return 403;
}

location /manager {
}

location /assets {
}

location / {
    try_files $uri /index.php?q=$uri&$args;

    ## other way:
    #if (!-f $request_filename) {
    #   rewrite ^(.*)$ /index.php?q=$1;
    #}
}
Winginx © Alexei Shabalin, 2011-2017