9. Where can I place rewrite
rules?
• httpd.conf (or included config files)
10. Where can I place rewrite
rules?
• httpd.conf (or included config files)
• VirtualHost
11. Where can I place rewrite
rules?
• httpd.conf (or included config files)
• VirtualHost
• Directory*
12. Where can I place rewrite
rules?
• httpd.conf (or included config files)
• VirtualHost
• Directory*
• .htaccess
13. Where can I place rewrite
rules?
• httpd.conf (or included config files)
• VirtualHost
• Directory*
• .htaccess
*Certain restrictions apply.**
14. Where can I place rewrite
rules?
• httpd.conf (or included config files)
• VirtualHost
• Directory*
• .htaccess
*Certain restrictions apply.**
**Use .htaccess files
24. Exhibit A, Explained
# Default WordPress rewrite rules
# Turn rewrite engine on
RewriteEngine On
# Base all rewrites on ‘/’ URL
RewriteBase /
# If the requested file does not exist
RewriteCond %{REQUEST_FILENAME} !-f
# ...or if no directory exists...
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite the URL to /index.php
RewriteRule . /index.php [L]
26. RewriteEngine on | off
• Enables or disables the runtime rewriting engine
27. RewriteEngine on | off
• Enables or disables the runtime rewriting engine
• Required inside VirtualHost and .htaccess
28. RewriteRule pattern substitution [flags]
• Can occur more than once
• Processed in order
• First rule processed on URL path
• Subsequent rules processed on previous output
• Think of chaining commands together in bash
50. RewriteRule pattern substitution [flags]
C Chain this rule to the next rule
E Set environment variable
F 403 Forbidden Status
G 410 Gone Status
H Specify handler
L Don’t process rules after this one
NC Case-insensitive
QSA Append query string
R[=301] Redirect (R=HTTP Status Code)
51. RewriteRule pattern substitution [flags]
C Chain this rule to the next rule
E Set environment variable
F 403 Forbidden Status
G 410 Gone Status
H Specify handler
L Don’t process rules after this one
NC Case-insensitive
QSA Append query string
R[=301] Redirect (R=HTTP Status Code)
52. RewriteRule pattern substitution [flags]
C Chain this rule to the next rule
E Set environment variable
F 403 Forbidden Status
G 410 Gone Status
H Specify handler
L Don’t process rules after this one
NC Case-insensitive
QSA Append query string
R[=301] Redirect (R=HTTP Status Code)
53. RewriteRule pattern substitution [flags]
C Chain this rule to the next rule
E Set environment variable
F 403 Forbidden Status
G 410 Gone Status
H Specify handler
L Don’t process rules after this one
NC Case-insensitive
QSA Append query string
R[=301] Redirect (R=HTTP Status Code)
54. RewriteRule pattern substitution [flags]
C Chain this rule to the next rule
E Set environment variable
F 403 Forbidden Status
G 410 Gone Status
H Specify handler
L Don’t process rules after this one
NC Case-insensitive
QSA Append query string
R[=301] Redirect (R=HTTP Status Code)
55. RewriteRule pattern substitution [flags]
C Chain this rule to the next rule
E Set environment variable
F 403 Forbidden Status
G 410 Gone Status
H Specify handler
L Don’t process rules after this one
NC Case-insensitive
QSA Append query string
R[=301] Redirect (R=HTTP Status Code)
56. RewriteRule pattern substitution [flags]
C Chain this rule to the next rule
E Set environment variable
F 403 Forbidden Status
G 410 Gone Status
H Specify handler
L Don’t process rules after this one
NC Case-insensitive
QSA Append query string
R[=301] Redirect (R=HTTP Status Code)
57. RewriteRule pattern substitution [flags]
C Chain this rule to the next rule
E Set environment variable
F 403 Forbidden Status
G 410 Gone Status
H Specify handler
L Don’t process rules after this one
NC Case-insensitive
QSA Append query string
R[=301] Redirect (R=HTTP Status Code)
58. RewriteRule pattern substitution [flags]
C Chain this rule to the next rule
E Set environment variable
F 403 Forbidden Status
G 410 Gone Status
H Specify handler
L Don’t process rules after this one
NC Case-insensitive
QSA Append query string
R[=301] Redirect (R=HTTP Status Code)
59. RewriteCond variable pattern [flags]
• HTTP Server Variables (HTTP_HOST, REQUEST_URI)
• Pattern that must match the given variable
• Optional flags that change the behavior, just like
RewriteRule
69. Exhibit A, Explained (Again)
# Default WordPress rewrite rules
# Turn rewrite engine on
RewriteEngine On
# Base all rewrites on ‘/’ URL
RewriteBase /
# If the requested file does not exist
RewriteCond %{REQUEST_FILENAME} !-f
# ...or if no directory exists...
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite the URL to /index.php
RewriteRule . /index.php [L]
70. non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Pattern Flags
Substitution
71. non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Match this pattern
Pattern inside parentheses
is capture as variable ‘$1’
72. non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Substitute it with “http://www.example.com”
73. non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
The pattern captured
inside the parentheses
74. non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
75. non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Redirect with a 301 Code
76. non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Make this the last RewriteRule
79. RewriteOptions inherit
• Inherit the configuration of the parent
• In per-virtual-server context, maps, conditions and rules
of the main server are inherited
80. RewriteOptions inherit
• Inherit the configuration of the parent
• In per-virtual-server context, maps, conditions and rules
of the main server are inherited
• In per-directory context, conditions and rules of the
parent directory's .htaccess configuration are inherited
83. Example
in httpd.conf
...
RewriteRule ^(.*)$ index.php
...
<VirtualHost 192.168.1.120:80>
ServerName zomgbacon.com
DocumentRoot /home/bacon/public_html
# Turn on the rewrite engine and inherit any rules
RewriteEngine On
RewriteOptions Inherit
</VirtualHost>
...
89. RewriteBase Example
in .htaccess file
#
# /abc/def/.htaccess -- per-dir config file for directory /abc/def
# Remember: /abc/def is the physical path of /xyz, i.e., the server
# has a 'Alias /xyz /abc/def' directive e.g.
#
RewriteEngine On
# let the server know that we were reached via /xyz and not
# via the physical path prefix /abc/def
RewriteBase /xyz
# now the rewriting rules
RewriteRule ^oldstuff.html$ newstuff.html
93. RewriteLog log-path
• Logs rewrites
• Level of logging can be tuned
• Relative paths are relative to DocumentRoot
94. RewriteLog log-path
• Logs rewrites
• Level of logging can be tuned
• Relative paths are relative to DocumentRoot
• Absolute paths are...well, absolute.