Difference between revisions of "URL Rewrite"
From Hostek.com Wiki
(→Application-Specific Rewrite Rules) |
(→Common Rewrite Rules) |
||
Line 156: | Line 156: | ||
=== Redirect to New Domain === | === Redirect to New Domain === | ||
+ | |||
+ | <source lang="apache"> | ||
+ | # Redirect to new domain | ||
+ | <IfModule mod_rewrite.c> | ||
+ | RewriteEngine On | ||
+ | RewriteCond %{HTTPS} off | ||
+ | RewriteCond %{HTTP_HOST} !^(www\.)?newdomain\.com$ [NC] | ||
+ | RewriteRule .* http://newdomain.com%{REQUEST_URI} [R=301,L] | ||
+ | RewriteCond %{HTTPS} on | ||
+ | RewriteCond %{HTTP_HOST} !^(www\.)?newdomain\.com$ [NC] | ||
+ | RewriteRule .* https://newdomain.com%{REQUEST_URI} [R=301,L] | ||
+ | </IfModule> | ||
+ | </source> | ||
+ | |||
=== Redirect Unsecure (HTTP) to Secure (HTTPS) === | === Redirect Unsecure (HTTP) to Secure (HTTPS) === | ||
+ | |||
+ | <source lang="apache"> | ||
+ | # Redirect to HTTPS | ||
+ | <IfModule mod_rewrite.c> | ||
+ | RewriteEngine On | ||
+ | RewriteCond %{HTTPS} off | ||
+ | RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L] | ||
+ | </IfModule> | ||
+ | </source> | ||
+ | |||
=== Redirect Mobile Users === | === Redirect Mobile Users === | ||
Line 196: | Line 220: | ||
RewriteRule ^.* - [F,L] | RewriteRule ^.* - [F,L] | ||
</IfModule> | </IfModule> | ||
+ | </source> | ||
+ | |||
+ | === Prevent Directory Listing === | ||
+ | |||
+ | <source lang="apache"> | ||
+ | # Prevent directory listing | ||
+ | Options All -Indexes | ||
+ | </source> | ||
+ | |||
+ | === Custom Error Pages === | ||
+ | |||
+ | <source lang="apache"> | ||
+ | # Custom error pages | ||
+ | ErrorDocument 400 /error400.html | ||
+ | ErrorDocument 401 /error401.html | ||
+ | ErrorDocument 403 /error403.html | ||
+ | ErrorDocument 404 /error403.html | ||
+ | ErrorDocument 500 /error500.html | ||
+ | </source> | ||
+ | |||
+ | === Follow Symbolic Links === | ||
+ | |||
+ | <source lang="apache"> | ||
+ | # Follow symbolic links | ||
+ | Options +FollowSymLinks | ||
+ | </source> | ||
+ | |||
+ | === Redirect to Maintenance Page === | ||
+ | |||
+ | <source lang="apache"> | ||
+ | # Redirect to maintenance page | ||
+ | <IfModule mod_rewrite.c> | ||
+ | RewriteCond %{REMOTE_ADDR} !^64\.207\.234\.198$ | ||
+ | RewriteCond %{REQUEST_URI} !^/maintenance\.html$ [NC] | ||
+ | RewriteCond %{REQUEST_URI} !\.(jpg|jpeg|png|gif|svg|swf|css|ico|js)$ [NC] | ||
+ | RewriteRule .* /maintenance.html [R=302,L] | ||
+ | </IfModule> | ||
+ | </source> | ||
+ | |||
+ | === Prevent Image Hotlinking === | ||
+ | |||
+ | <source lang="apache"> | ||
+ | # Prevent hotlinking | ||
+ | <IfModule mod_rewrite.c> | ||
+ | RewriteCond %{HTTP_REFERER} !^$ | ||
+ | RewriteCond %{HTTP_REFERER} ^https?://([^/]+)/ [NC] | ||
+ | RewriteCond %1#%{HTTP_HOST} !^(.+)#\1$ | ||
+ | RewriteRule \.(jpg|jpeg|png|gif|swf|svg)$ - [NC,F,L] | ||
+ | </IfModule> | ||
+ | </source> | ||
+ | |||
+ | === Compress Common Filetypes === | ||
+ | |||
+ | <source lang="apache"> | ||
+ | # Compress common filetypes | ||
+ | AddOutputFilterByType DEFLATE text/plain | ||
+ | AddOutputFilterByType DEFLATE text/html | ||
+ | AddOutputFilterByType DEFLATE text/xml | ||
+ | AddOutputFilterByType DEFLATE text/css | ||
+ | AddOutputFilterByType DEFLATE application/xml | ||
+ | AddOutputFilterByType DEFLATE application/xhtml+xml | ||
+ | AddOutputFilterByType DEFLATE application/rss+xml | ||
+ | AddOutputFilterByType DEFLATE application/javascript | ||
+ | AddOutputFilterByType DEFLATE application/x-javascript | ||
</source> | </source> | ||
Revision as of 10:59, 16 July 2015
Contents
- 1 Overview
- 2 Platform Compatibility
- 3 Apache (Linux)
- 3.1 Directives - RewriteEngine
- 3.2 Directives - Rewrite Options
- 3.3 Directives - RewriteLog
- 3.4 Directives - RewriteLogLevel
- 3.5 Directives - RewriteLock
- 3.6 Directives - RewriteMap
- 3.7 Directives - RewriteBase
- 3.8 Directives - RewriteCond
- 3.9 Directives - RewriteRule
- 3.10 Flags - RewriteRules
- 3.11 Flags - RewriteCond
- 3.12 Variables - HTTP Headers
- 3.13 Variables - Request
- 3.14 Variables - Server Internals
- 3.15 Variables - Special
- 3.16 Variables - Time
- 3.17 <IfModule>
- 3.18 Common Issues - Filename
- 3.19 Common Issues - AllowOverride
- 3.20 Common Issues - Syntax
- 3.21 Common Issues - Special Characters
- 3.22 Common Issues - Nesting
- 3.23 Additional Resources
- 4 IIS (Windows)
- 5 ISAPIRewrite (Windows)
- 6 Redirect Codes
- 7 Regular Expressions
- 8 Common Rewrite Rules
- 8.1 Redirect to www
- 8.2 Redirect to non-www
- 8.3 Redirect Single Page
- 8.4 Redirect Entire Site
- 8.5 Redirect Entire Site with Query String
- 8.6 Redirect Entire Site to Sub-Folder
- 8.7 Redirect Sub-Folder to Different Site
- 8.8 Redirect to Different File Extension
- 8.9 Redirect to New Domain
- 8.10 Redirect Unsecure (HTTP) to Secure (HTTPS)
- 8.11 Redirect Mobile Users
- 8.12 Block IP Addresses
- 8.13 Block All Except IP Addresses
- 8.14 Block Bad Bots
- 8.15 Prevent Directory Listing
- 8.16 Custom Error Pages
- 8.17 Follow Symbolic Links
- 8.18 Redirect to Maintenance Page
- 8.19 Prevent Image Hotlinking
- 8.20 Compress Common Filetypes
- 9 Application-Specific Rewrite Rules
Overview
Platform Compatibility
Apache (Linux)
Directives - RewriteEngine
Directives - Rewrite Options
Directives - RewriteLog
Directives - RewriteLogLevel
Directives - RewriteLock
Directives - RewriteMap
Directives - RewriteBase
Directives - RewriteCond
Directives - RewriteRule
Flags - RewriteRules
Flags - RewriteCond
Variables - HTTP Headers
Variables - Request
Variables - Server Internals
Variables - Special
Variables - Time
<IfModule>
Common Issues - Filename
Common Issues - AllowOverride
Common Issues - Syntax
Common Issues - Special Characters
Common Issues - Nesting
Additional Resources
IIS (Windows)
Inbound Rules
Outbound Rules
Inheritance
Rewrite Conditions
Server Variables
String Functions
Rewrite Maps
Externalization of Rewrite Rules
Additional Resources
- URL Rewrite Community Forum
ISAPIRewrite (Windows)
Unsupported Features
Common Issues - Leading Slashes
Common Issues - Windows Filename Requirements
Additional Resources
- ISAPI_Rewrite 3
- ISAPI_Rewrite 3 - Documentation
Redirect Codes
301 - Moved Permanently
302 - Found
303 - See Other
307 - Temporary Redirect
308 - Permanent Redirect
Regular Expressions
Anchors
Character Classes
Character Classes - POSIX
Assertions
Quantifiers
Escape Sequences
Common Meta Characters
Special Characters
Groups and Ranges
Pattern Modifiers
String Replacement
Testing Tools
Below is a brief list of online tools to create and test regular expressions.
- Regular Expressions 101
- RegExr
- RegexPal
- Rubular
Additional Resources
Below is a list of useful online resource to help you learn and familiarize yourself with regular expressions.
- Regular Expression (Wikipedia)
- Regular Expressions
- Regular Expressions (Mozilla)
- JavaScrip RegExp Reference
Common Rewrite Rules
Redirect to www
# Add WWW Prefix <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_HOST} ^example\.com [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC] </IfModule>
Redirect to non-www
# Remove WWW Prefix <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_HOST} ^www\.example\.com [NC] RewriteRule ^(.*)$ http://example.com/$1 [L,R=301] </IfModule>
Redirect Single Page
# Redirect page Redirect 301 /old.html /new.html
Redirect Entire Site
# Redirect entire domain Redirect 301 / http://www.example.com
Redirect Entire Site with Query String
Redirect Entire Site to Sub-Folder
Redirect Sub-Folder to Different Site
Redirect to Different File Extension
# Redirect to new file extension RedirectMatch 301 (.*)\.html$ http://www.example.com$1.php
Redirect to New Domain
# Redirect to new domain <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} !^(www\.)?newdomain\.com$ [NC] RewriteRule .* http://newdomain.com%{REQUEST_URI} [R=301,L] RewriteCond %{HTTPS} on RewriteCond %{HTTP_HOST} !^(www\.)?newdomain\.com$ [NC] RewriteRule .* https://newdomain.com%{REQUEST_URI} [R=301,L] </IfModule>
Redirect Unsecure (HTTP) to Secure (HTTPS)
# Redirect to HTTPS <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L] </IfModule>
Redirect Mobile Users
# Redirect mobile devices <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_ACCEPT} "text\/vnd\.wap\.wml|application\/vnd\.wap\.xhtml\+xml" [NC] RewriteCond %{REQUEST_URI} ^/$ RewriteRule ^ http://m.example.com%{REQUEST_URI} [R,L] </IfModule>
Block IP Addresses
# Block IP addresses Order Deny,Allow Deny from 1.2.3.4
Block All Except IP Addresses
# Block all except IP addresses Order Deny,Allow Deny from all Allow from 1.2.3.4
Block Bad Bots
# Block bad bots <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_USER_AGENT} ^Bot [OR] RewriteCond %{HTTP_USER_AGENT} ^AnotherBotName RewriteRule ^.* - [F,L] </IfModule>
Prevent Directory Listing
# Prevent directory listing Options All -Indexes
Custom Error Pages
# Custom error pages ErrorDocument 400 /error400.html ErrorDocument 401 /error401.html ErrorDocument 403 /error403.html ErrorDocument 404 /error403.html ErrorDocument 500 /error500.html
Follow Symbolic Links
# Follow symbolic links Options +FollowSymLinks
Redirect to Maintenance Page
# Redirect to maintenance page <IfModule mod_rewrite.c> RewriteCond %{REMOTE_ADDR} !^64\.207\.234\.198$ RewriteCond %{REQUEST_URI} !^/maintenance\.html$ [NC] RewriteCond %{REQUEST_URI} !\.(jpg|jpeg|png|gif|svg|swf|css|ico|js)$ [NC] RewriteRule .* /maintenance.html [R=302,L] </IfModule>
Prevent Image Hotlinking
# Prevent hotlinking <IfModule mod_rewrite.c> RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} ^https?://([^/]+)/ [NC] RewriteCond %1#%{HTTP_HOST} !^(.+)#\1$ RewriteRule \.(jpg|jpeg|png|gif|swf|svg)$ - [NC,F,L] </IfModule>
Compress Common Filetypes
# Compress common filetypes AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/x-javascript
Application-Specific Rewrite Rules
CakePHP
CodeIgniter
# CodeIgniter permalinks <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] </IfModule>
concrete5
# Concrete5 permalinks <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] </IfModule>
DotNetNuke
Joomla
# Joomla permalinks <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_URI} !^/index\.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* index.php [L] </IfModule>
Magento
Mura
WordPress
To enable permalinks within WordPress:
- Login to your WordPress dashboard
- Hover over Settings on the menu
- Choose Permalinks from the menu that appears
- Choose one of the available permalink structures
- Click the Save Changes button
Linux (.htaccess)
This file is generated by WordPress automatically when Permalinks are enabled using the instructions above. It is provided here for reference.
# Wordpress permalinks <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule>
Windows (web.config)
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="WordPress Permalinks - Homepage" stopProcessing="true"> <match url="^index\.php$" /> <action type="None" /> </rule> <rule name="WordPress Permalinks - All Requests" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="index.php" /> </rule> </rules> </rewrite> </system.webServer> </configuration>