BestDivision Logo

A complete guide for Understanding .htaccess file and its codes

BestDivision
BestDivision  @bestdivision
Created At - 2020-09-20
Last Updated - 2024-08-28

Table of Contents

  • A complete guide for understanding .htaccess file and its code.
    • Understanding Regular Expression

A complete guide for understanding .htaccess file and its code.

Understanding Regular Expression

**1) Period ** . :

This matches any single character.

Example a): c.t will match cat, cbt, cct, and so on?

2) Plus +:

This will repeat the previous match one or more times.

Example a): _a+_ matches _a, aa, aaa,_ and so on...

Example b): _(ab)+_ matches _(ab), (abab), (ababab)_, and so on?

3) Asterisk *:

This will repeat the previous match zero or more times.

Example a): _a*_ matches, empty strings, _a, aa, aaa,_ and so on...

Example b): (_ab)*_ matches empty strings**,** _ab, abab, ababab,_ and so on?

4) Question ?:

This will do an optional match.

**Example a): ** colou? r  will match color and colour.

5) Backslash \:

This will escape the next character.

Example a): \. will match .(dot) and not any single character.

6) Tilde (or anchor) ^: 

This will match the beginning of the string

**Example a): ** ^a will match any string starting with a.

7) Dollar Sign $:

This will match the beginning of the string

Example a): a$ will match any string ending with a.

8) Parenthesis ( ):

This will group several characters into a single unit.

Example a): _(ab)+_ matches _(ab), (abab), (ababab),_ and so on?

Example b): _(ab)*_ matches empty strings**,** _ab, abab, ababab,_ and so on?

8) Bracket **[ ]**:

This will match one of the characters.

Example a): c[abc]t will match cat, cbt. cct.

9) Bracket [ ^ ]:

This is negative character class and will match any character which is not present there.

Example a): c[^a]t will not match cat but will match cbt, cct, cdt, etc.

mod_rewrite an Apache module which allows us to rewrite URL requests that come into our server.

Uses: 

  1. Removing index.php in Codeigniter Project.
  2. Forcing https://
  3. Convert URL to lowercase.
  4. Forcing www for each request.
  5. Redirecting from one subdirectory to a different one.
    1. https://example.com/folder1  ==> https://example.com/folder2
  6. Redirect request to a subdirectory
    1. https://example.com/image.jpg ==> https://example.com/folder/image.jpg
Share

‌

  • ‌

    ‌
    ‌

    ‌

    ‌
  • ‌

    ‌
    ‌

    ‌

    ‌
  • ‌

    ‌
    ‌

    ‌

    ‌
  • ‌

    ‌
    ‌

    ‌

    ‌
  • ‌

    ‌
    ‌

    ‌

    ‌
  • ‌

    ‌
    ‌

    ‌

    ‌
  • ‌

    ‌
    ‌

    ‌

    ‌
  • ‌

    ‌
    ‌

    ‌

    ‌
  • ‌

    ‌
    ‌

    ‌

    ‌