“Imagination is everything. It is the preview of life's coming attractions”

- Albert Einstein

  • Htaccess Redirects With Certain URI And Query Parameters


    Htaccess Redirects With Certain Uri And Query Parameters

    Handling redirects using htaccess seems intuitive and easy. But sometimes when you need to quickly fix a nasty URL containing some badly composed URI and not SEO friendly query parameters things may get out of hand. To prevent you from crying all day long and wasting time on asking yourself about sense in all of this - let’s solve that problem with a style.

    Let’s say that the we want to redirect user from one address, let's say www.example.com/content/content.php?content,1 to www.example.com/index.php, dropping the query on redirect. Easiest solution would look like this:

    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/content/content\.php$
    RewriteCond %{QUERY_STRING} ^content,1$
    RewriteRule .* $ http://www.example.com/index.php? [R=301]
    

    So basically, what are we doing here? Let’s sum everything up in points:

    1. First line enables RewriteEngine, which basically allows all instructions below to be executed in this .htaccess file and all other .htaccess files in this folder’s subdirectories (of course only if subdirectory is not a seperate subdomain - then it has it’s own root rewrites).
    2. Second line compares REQUEST_URI variable (containing everything that comes between HTTP_HOST and QUEST_STRING, eg. /content/content.php in our example) with ^/content/content\.php$ regular expression. Let’s explain it:
      • the ^ at the beginning ensures that REQUEST_URI must start with /content/content.php to match this expression,
      • \. means escaped dot. Dot in PCRE expressions is equivalent of one character of any kind, be it number, letter or special character. Escaping the dot means that we want a dot character to be in that place of REQUEST_URI,
      • $ means that REQUEST_URI will only match this expression after /content/content.php there are no more characters in the URI.
      This RewriteCond is connected with an [AND] flag by default with third line.
    3. Third line compares QUERY_STRING (everything after ?character in the URL, in our example that would be content,1). If this and previous RewriteCond are true (both of them must be) then the RewriteRule located in the fourth line takes place.
    4. In the fourth line pattern is matched by default against the part of the URL that comes after the hostname and before the query string - which is basically URI. Pattern .* means that it matches with 0 to N long string consisting of character of any kind . Parameters in the square brackets to the right are called “flags”. Multiple flags can be used, separated by a comma, eg. [L,R=301]. In our example R flag is used, which means that user will be redirected to the http://www.example.com/index.php with HTTP status code 301 (moved permanently). ? character at the end of redirection address means that no query parameters will be added. Otherwise we would be redirected to http://www.example.com/index.php?content,1.

    So that basically covers it. Questions? No? Thought so.

    Back