Word Count: 473
  • Post category:Servers
  • Post last modified:2023-08-29

Introduction

This article is an extract from the 2 hours long video in here.

To use mod-rewrite, the regular expression has to be covered briefly first.

Regular Expression

Regular Express is use to parase character by character.

Online tool: regexr.com

Regular Expression – Meta Characters

Common Meta characters Meaning
^start of the pattern, begins with the following character.
$end of the pattern, ends with the preceding character.
.dotmatches any characters
\backslashescape the following character so it become the literal character.
?quantity metapreceding char has zero or 1 occurance
*quantity meta asterisk preceding char has zero or more occurances
+quantity meta preceding char has 1 or more occurances
.+one or more occurances of any character
( )groupinggrouping which can be put into a variable, e.g. $1
(com|net|org)either one in the grouping can match
[a-z]
[0-9]
[A-Z]
character class–> any one lower case character can match
–> any digit can match
–> any one uppercase character can match
!negation characterthe oppsite of the matched pattern
\wmatch a word, similar to [a-zA-Z0-9]
\dmatch an integer number, similar to [0-9]
\d{3}three matches of the preceding pattern
\d{1, 3}at least 1 match and at most 3 matches

MOD_REWRITE VARIABLES

The Mod_Rewrite Variables Cheatsheet provides useful resources in producing rewrite rules.

https://www.askapache.com/htaccess/mod_rewrite-variables-cheatsheet/

MOD_REWRITE

RewriteRule

RewriteEngine On

# Rule syntax:
# RewriteRule Pattern Substituition [flag,...]
# Where Pattern is the input URL that is matched
# Where Substitution is the replacement or revision to the URL
# Where flag is a series of behaviur options for mod_rewrite

RewriteRule ^boats/([0-9])+$ http://myboats.com/boats/$1 [L]
# the domain of the Pattern http://mysite.com is implicit.
# OR
RewriteRule ^boats/(\d+)$ http://myboats.com/boats/$1 [C]
RewriteRule ^([a-zA-Z]+)/(\d+)$ http://myboats.com/products/$1/$2 [L]

# [C] - chain rules
# [L] - last rule

RewriteCond

RewriteEngine On

# RewriteCond Test Pattern [flag,...]
# Where Test is a string with or without variables like %{REQUEST_URI}
# Where Pattern is the Regular Expression test for string match
# Where flag is a series of behaviur options for mod_rewrite
# For more than 1 condition, all conditions must be true for running the rewrite rule.

RewriteCond %{HTTP_HOST} ^www\.my(\w+)\.com$ [NC]
RewriteCond %1 !^boats$

# The %1 refers to the first grouping in the previous condition test.

RewriteRule ^([a-zA-Z]+)/(\d+)$ http://myboats.com/products/$1/$2 [L]

# [C] - chain, where output of one rule becomes the input of the next
# [L] - last rule
# [NC] - no case (i.e., case insensitive)
# [OR] - or condition

RewriteBase

The first file is /var/www/mysite/index.html
The second file is /var/www/mysite/boats/boats.html
if we want to redirect index.html to boats.html, we could write:
RewriteRule ^index\.html$ /boats/boats.html

If we change the RewriteBase to /boats, the rule can be simplfied as:
RewriteBase /boats
RewriteRule ^index\.html$ boats.html [L]