Skip to content

Instantly share code, notes, and snippets.

@lparolari
Last active October 30, 2018 20:16
Show Gist options
  • Save lparolari/a519827810b9b766c3f92ca7293398e5 to your computer and use it in GitHub Desktop.
Save lparolari/a519827810b9b766c3f92ca7293398e5 to your computer and use it in GitHub Desktop.
A rewrite url memo with quick tips and description for basic functionalities

htaccess rewrite-url memo

Luca Parolari <luca.parolari23@gmail.com>

The .htaccess file is a config file used to change the behavior of the web server when entering a specific directory, in particular the directory that contains .htaccess file.

The file is readed every time a request is issued to the server. For this reason, changes to the file does not implies the restart of the server.

Rewrite Url

The rewrite url functionality is very powerful. It can translate a given url to another according to some rules ans some conditional statemets.

This, in order to work, needs the mod_rewrite enabled on apache

sudo a2enmod rewrite

This needs the server restart

sudo /etc/init.d/apache2 restart

Rewrite mode scaffolding

Every rewrite rule should be writte inside this code, that checks if the module is loaded and the executes the rewrite rules.

<IfModule mod_rewrite.c>
    ...
</IfModule>

Note: following rules and/or conditions, if not specified are assumed to be inside the IfModule section.

Enabling rewrite mode

For enabling rewrite url mode you need to add a statement that tells to your server to process the following lines and translate urls.

RewriteEngine On

This could be useful in testing environments.

Conditional statements

Sometimes you want to rewrite urls only if some conditions are satified, for example:

  • you are (not) requesting for a filename;
  • your url to be rewritten must contains a special word;
  • etc.

Conditinal statements are created by teh keyword RewriteCond, followed by the variable to evaluate and then the test condition.

# matches all urls which are not requesting for a filename (`!-f` -
#  not filename - option).
RewriteCond %{REQUEST_FILENAME} !-f

or

# matches all http auth headers.
RewriteCond %{HTTP:Authorization} ^(.*)

Rewrite rules

A rewrite rules tells to the engin how the url should be translated.

RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]

Where:

  • RewriteRule is the keyword for the rewrite rule;
  • ^(.*)$ is a "capture";
  • index.php?_url=/$1 is how to rewrite the url;
  • [QSA,L] are some flags.

Caputers

A capture allows to capture a portion of url.

Captures are (usually) started by ^ and terminated by $. Can be useful to make the regex readable.

The (, ) pair is used to encapsulate portions of url into a variable. In particular, every portion of url captured by round brackets is provided in a variable $i (i is a natural number) in the rewrite rule section of the RewriteRule command.

Inside round brackes we can put special characters, used to caputre portions of url. This special chars are:

  • ., any character (even none, but no more than one);
  • *, zero, one or more than one character;
  • string, a general string: is used to match literal portion of url

Captures examples

Url1: www.mywebapi.com/v0/auth

Url2: www.mywebapi.com/v1/auth

Now I want to redirect v0 url request to a index.php file in it's folder, I just have to

RewriteUrl ^(.*v0)$ $1/index.php?_url=$2

and the rewrited url should be

www.mywebapi.com/v0/index.php?_url=/auth

Note that with this rewrite url, the "v1" url is not rewrited.

Note: to verify this you can use online tools like this

You can use more than one round brackes to capture different portions of url and rewrite it more intelligently.

Full examples

<IfModule mod_rewrite.c>

    # Enable the engine.
    RewriteEngine On

    # Condition: I'm not asking for a file, but for a path.
    ## Can be something like /auth/token/, /auth/token, ...
    ## Can be even directory that not exists.
    RewriteCond %{REQUEST_FILENAME} !-f

    # Rewriting rule: translating old url to the new.
    RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]

    # Other translations (you should google this to understand better)
    RewriteCond %{HTTP:Authorization} ^(.*)
    RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment