Uses
• Obscure original URLs
• Clean URLs
• SEO (Seriously.)
Uses
• Obscure original URLs
• Clean URLs
• SEO (Seriously.)
• Redirection
Uses
• Obscure original URLs
• Clean URLs
• SEO (Seriously.)
• Redirection
• Access control
Where can I place rewrite
rules?
Where can I place rewrite
rules?
• httpd.conf (or included config files)
Where can I place rewrite
rules?
• httpd.conf (or included config files)
• VirtualHost
Where can I place rewrite
rules?
• httpd.conf (or included config files)
• VirtualHost
• Directory*
Where can I place rewrite
rules?
• httpd.conf (or included config files)
• VirtualHost
• Directory*
• .htaccess
Where can I place rewrite
rules?
• httpd.conf (or included config files)
• VirtualHost
• Directory*
• .htaccess
*Certain restrictions apply.**
Where can I place rewrite
rules?
• httpd.conf (or included config files)
• VirtualHost
• Directory*
• .htaccess
*Certain restrictions apply.**
**Use .htaccess files
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]
RewriteEngine on | off
RewriteEngine on | off
• Enables or disables the runtime rewriting engine
RewriteEngine on | off
• Enables or disables the runtime rewriting engine
• Required inside VirtualHost and .htaccess
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
RewriteRule pattern substitution [flags]
Can be one of several things:
RewriteRule pattern substitution [flags]
Can be one of several things:
• file-system path
RewriteRule pattern substitution [flags]
Can be one of several things:
• file-system path
• URL path
RewriteRule pattern substitution [flags]
Can be one of several things:
• file-system path
• URL path
• Absolute URL
RewriteRule pattern substitution [flags]
Can be one of several things:
• file-system path
• URL path
• Absolute URL
• - [dash] (no substitution)
RewriteRule pattern substitution [flags]
RewriteRule pattern substitution [flags]
• Affect behavior of the Rule or Condition
RewriteRule pattern substitution [flags]
• Affect behavior of the Rule or Condition
• Contained in square brackets
RewriteRule pattern substitution [flags]
• Affect behavior of the Rule or Condition
• Contained in square brackets
• Comma-separated
RewriteRule pattern substitution [flags]
• Affect behavior of the Rule or Condition
• Contained in square brackets [NC]
• Comma-separated
RewriteRule pattern substitution [flags]
• Affect behavior of the Rule or Condition
• Contained in square brackets [NC]
• Comma-separated [NC, QSA, L]
RewriteRule pattern substitution [flags]
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)
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)
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)
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)
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)
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)
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)
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)
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)
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
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]
non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Pattern Flags
Substitution
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’
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”
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
non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
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
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
RewriteOptions inherit
RewriteOptions inherit
• Inherit the configuration of the parent
RewriteOptions inherit
• Inherit the configuration of the parent
• In per-virtual-server context, maps, conditions and rules
of the main server are inherited
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
Example
Example
in httpd.conf
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>
...
RewriteBase URL-path
RewriteBase URL-path
• Sets the base URL for per-directory rewrites
RewriteBase URL-path
• Sets the base URL for per-directory rewrites
• URLs are NOT directly related to physical filename
paths
RewriteBase Example
RewriteBase Example
in .htaccess file
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
RewriteLog log-path
RewriteLog log-path
• Logs rewrites
RewriteLog log-path
• Logs rewrites
• Level of logging can be tuned
RewriteLog log-path
• Logs rewrites
• Level of logging can be tuned
• Relative paths are relative to DocumentRoot
RewriteLog log-path
• Logs rewrites
• Level of logging can be tuned
• Relative paths are relative to DocumentRoot
• Absolute paths are...well, absolute.
RewriteLogLevel level
RewriteLogLevel level
• Integer value 0-9
RewriteLogLevel level
• Integer value 0-9
• 0 == disabled
0 comments
Post a comment