Difference between revisions of "URL Rewrite"

From Hostek.com Wiki
Jump to: navigation, search
(Redirect Single Page)
(Redirect Entire Site)
Line 98: Line 98:
  
 
=== Redirect Entire Site ===
 
=== Redirect Entire Site ===
 +
 +
<source lang="apache">
 +
# Redirect entire domain
 +
Redirect 301 / http://www.example.com
 +
</source>
 +
 
=== Redirect Entire Site with Query String ===
 
=== Redirect Entire Site with Query String ===
 
=== Redirect Entire Site to Sub-Folder ===
 
=== Redirect Entire Site to Sub-Folder ===

Revision as of 08:55, 16 July 2015

Contents

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

ISAPIRewrite (Windows)

Unsupported Features

Common Issues - Leading Slashes

Common Issues - Windows Filename Requirements

Additional Resources

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

Additional Resources

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 Domain

Redirect Unsecure (HTTP) to Secure (HTTPS)

Redirect Mobile Users

Block IP Addresses

Block Bad Bots

Application-Specific Rewrite Rules

CakePHP

CodeIgniter

concrete5

DotNetNuke

Joomla

Magento

Mura

osCommerce

WordPress

To enable permalinks within WordPress:

  1. Login to your WordPress dashboard
  2. Hover over Settings on the menu
  3. Choose Permalinks from the menu that appears
  4. Choose one of the available permalink structures
  5. Click the Save Changes button

Wordpress-rewrite-01.png Wordpress-rewrite-02.png Wordpress-rewrite-03.png

Linux (.htaccess)

This file is generated by WordPress automatically when Permalinks are enabled using the instructions above. It is provided here for reference.

<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>