Apache Rewrite Rules

42 %
58 %
Information about Apache Rewrite Rules
Technology

Published on November 28, 2008

Author: bansalakhil

Source: slideshare.net

Description

Basic talk about apache mod_rewrite

Apache Re-write Rules

What? Powerful mechanism Hiding Actual URLs Redirecting Reformatting request URLs

Powerful mechanism

Hiding Actual URLs

Redirecting

Reformatting request URLs

Why? Search engine friendly URLs User Friendly URLs Easy to remember Hide underlying technology index.php, index.axp, index.pl Hide actual path of files

Search engine friendly URLs

User Friendly URLs

Easy to remember

Hide underlying technology

index.php, index.axp, index.pl

Hide actual path of files

What we need? Apache compiled with mod_rewrite module mod_rewrite module should be enabled RewriteEngine on

Apache compiled with mod_rewrite module

mod_rewrite module should be enabled

RewriteEngine on

What does mod_rewrite do? It catches URLs that meet specific conditions, and rewrites them as it was told to do.

It catches URLs that meet specific

conditions, and rewrites them as it was

told to do.

But how does it do that? Example: RewriteRule ^/shortcut$ /complicated/url/here <IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^/shortcut$ /complicated/url/here </IfModule>

Example:

RewriteRule ^/shortcut$ /complicated/url/here

<IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^/shortcut$ /complicated/url/here

</IfModule>

The Structure of RewriteRule RewriteRule Pattern Substitution [OptionalFlags]

RewriteRule Pattern Substitution [OptionalFlags]

What are OptionalFlags? Not mandatory Surrounded in square brackets Comma separated Commonly used flags: F - Forbidden. The user will receive a 403 error. L - Last Rule. No more rules will be proccessed if this one was successful. R[=code] – Redirect. NC – No Case

Not mandatory

Surrounded in square brackets

Comma separated

Commonly used flags:

F - Forbidden. The user will receive a 403 error.

L - Last Rule. No more rules will be proccessed if this one was successful.

R[=code] – Redirect.

NC – No Case

More condition using RewriteCond RewriteCond directive defines a rule condition Preserve RewriteRule with one or more RewriteCond directives RewriteEngine On RewriteCond %{HTTP_REFERER} !^$ [NC] RewriteCond %{HTTP_REFERER} !^http://domain.com [NC] RewriteCond %{HTTP_REFERER} !^http://www.domain.com [NC] RewriteCond %{HTTP_REFERER} !^http://xxx.yyy.zzz.100 [NC] RewriteRule ^.*$ http://www.domain.com/ [R,L]

RewriteCond directive defines a rule condition

Preserve RewriteRule with one or more RewriteCond directives

RewriteEngine On

RewriteCond %{HTTP_REFERER} !^$ [NC]

RewriteCond %{HTTP_REFERER} !^http://domain.com [NC]

RewriteCond %{HTTP_REFERER} !^http://www.domain.com [NC]

RewriteCond %{HTTP_REFERER} !^http://xxx.yyy.zzz.100 [NC]

RewriteRule ^.*$ http://www.domain.com/ [R,L]

Control Flow

Server Variables %{ NAME_OF_VARIABLE } HTTP_USER_AGENT HTTP_REFERER, HTTP_HOST REQUEST_METHOD REMOTE_HOST QUERY_STRING HTTPS SERVER_PORT etc...

%{ NAME_OF_VARIABLE }

HTTP_USER_AGENT

HTTP_REFERER,

HTTP_HOST

REQUEST_METHOD

REMOTE_HOST

QUERY_STRING

HTTPS

SERVER_PORT

etc...

Some Examples: RewriteRule ^products/([0-9][0-9])/$ /product.php?prodID=$1 RewriteCond %{HTTP_HOST} !^www.example.com$ [NC] RewriteRule .? http://www.example.com%{REQUEST_URI} [R=301,L] RewriteRule ^/blog/?(.*)$ http://blog.domain.com/$1 [P,NC,L] RewriteCond %{TIME_HOUR}%{TIME_MIN} >0700 RewriteCond %{TIME_HOUR}%{TIME_MIN} <1900 RewriteRule ^foo.html$ foo.day.html RewriteRule ^foo.html$ foo.night.html

RewriteRule ^products/([0-9][0-9])/$ /product.php?prodID=$1

RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]

RewriteRule .? http://www.example.com%{REQUEST_URI} [R=301,L]

RewriteRule ^/blog/?(.*)$ http://blog.domain.com/$1 [P,NC,L]

RewriteCond %{TIME_HOUR}%{TIME_MIN} >0700

RewriteCond %{TIME_HOUR}%{TIME_MIN} <1900

RewriteRule ^foo.html$ foo.day.html

RewriteRule ^foo.html$ foo.night.html

Thanks :-) http://webonrails.com

Thanks

:-)

http://webonrails.com

Add a comment

Related presentations

Related pages

mod_rewrite - Apache HTTP Server Version 2.4

The mod_rewrite module uses a rule-based rewriting engine, based on a PCRE regular-expression parser, to rewrite requested URLs on the fly. By default, mod ...
Read more

Rewrite-Engine – Wikipedia

Eine Rewrite-Engine (von englisch rewrite, „umschreiben“ und engine, „Maschine“) ... Apache/mod_rewrite RewriteEngine on RewriteRule ...
Read more

Beispiele | modrewrite.de

mod_rewrite & .htaccess. Home; mod_rewrite.htaccess; support-forum; modrewrite.de. Home; mod_rewrite; Anwendungen; Syntax; Beispiele; datei.html statt ...
Read more

RewriteRule - Httpd Wiki

RewriteRule is the main directive (heart) of mod_rewrite. Syntax. Overview. The directive consists of three arguments, separated by a space ...
Read more

Importing Apache mod_rewrite Rules : The Official ...

The URL Rewrite Module in IIS 7 and above provides an import feature that greatly simplifies the process of converting Apache mod_rewrite rules to IIS URL ...
Read more

How To Set Up Mod_Rewrite | DigitalOcean

This tutorial goes over Mod_Rewrite, an apache module that allows you to manipulate URLS to ... How To Set Up Mod_Rewrite ... How to Use Rewrite Rules;
Read more

RewriteHTTPToHTTPS - Httpd Wiki

RewriteHTTPToHTTPS; Immutable Page; ... Using mod_rewrite to do this isn't the recommended ... While the rules you need are the same as ...
Read more

mod_rewrite / Suchmaschinenoptimierung / .htaccess ...

mod_rewrite ist ein Apache Modul für die URL Manipulation. Mit der RewriteEngine des Apache-Webservers ist es möglich die angeforderte URL anhand von ...
Read more