background preloader

Mod_rewrite

Mod_rewrite
Available Languages: en | fr Summary 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_rewrite maps a URL to a filesystem path. mod_rewrite provides a flexible and powerful way to manipulate URLs using an unlimited number of rules. mod_rewrite operates on the full URL path, including the path-info section. Further details, discussion, and examples, are provided in the detailed mod_rewrite documentation. Logging mod_rewrite offers detailed logging of its actions at the trace1 to trace8 log levels. Using a high trace log level for mod_rewrite will slow down your Apache HTTP Server dramatically! Example LogLevel alert rewrite:trace3 RewriteLog Those familiar with earlier versions of mod_rewrite will no doubt be looking for the RewriteLog and RewriteLogLevel directives. To get just the mod_rewrite-specific log messages, pipe the log file through grep: tail -f error_log|fgrep '[rewrite:' txt

Apache 2.2 module mod_macro Contents Summary mod_macro is a third-party module to the Apache Http Server, distributed with a BSD-style license like Apache. Examples Here is a sample use of mod_macro within a configuration file: ## Define a VHost Macro for repetitive configurations <Macro VHost $host $port $dir> Listen $port <VirtualHost *:$port> ServerName $host DocumentRoot $dir <Directory $dir> # do something here... A simple example is presented in issue 144 of Apache Week. Another example shows how to simplify mod_perl configuration with mod_macro. Some promotion by Jim Jagielski at ApacheCon reported on Trouble Shooters by Steve Litt. A blog post by Patrick Allaert who noted that the macro definition does not need to be perfectly nested. Another blog post by Mads Sülau Jørgensen presents the module. One more blog post by David Anderson who presents a neat configuration. An article by Jeppe Toustrup with advanced parametric macros, that is a macro argument is used as a macro name in the definition. Benefits Author

Mod_Rewrite Variables Cheatsheet We've figured out what mod_rewrite variables look like so we can create rewrite rules and condition patterns based on the actual value. This cheatsheet is where we'll lay them all out for quick reference. This cheatsheet changed my life. Here the focus is on identifying mod_rewrite variables and defining the limits of the module by checking the mod_rewrite source code. List of Mod_Rewrite Variables ^ HTTP Variables SSL Variables Variable Values and Examples ^ RewriteCond %{API_VERSION} ^(.*)$ RewriteRule .* AUTH_TYPE: Digest RewriteRule .* - [E=IN_AUTH_TYPE:%{AUTH_TYPE}] RequestHeader set AUTH_TYPE "%{IN_AUTH_TYPE}e" CACHE_CONTROL: max-age=0 CONNECTION: keep-alive CONTENT_LENGTH: (null) CONTENT_TYPE: (null) DOCUMENT_ROOT: /web/webroot/askapache.com RewriteCond %{DOCUMENT_ROOT}/cache%{REQUEST_URI}/index.html -f RewriteRule . HOST: www.askapache.com HTTP_ACCEPT: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 RewriteCond %{HTTP_COOKIE} ^. IS_SUBREQ: false pattern

Htaccess Rewrites – Rewrite Tricks and Tips Htaccess Rewrites are enabled by using the Apache module mod_rewrite, which is one of the most powerful Apache modules and features availale. Htaccess Rewrites through mod_rewrite provide the special ability to Rewrite requests internally as well as Redirect request externally. When the url in your browser's location bar stays the same for a request it is an internal rewrite, when the url changes an external redirection is taking place. Despite the tons of examples and docs, mod_rewrite is voodoo.Damned cool voodoo, but still voodoo.-- Brian Moore Note: After years of fighting to learn my way through rewriting urls with mod_rewrite, I finally had a breakthrough and found a way to outsmart the difficulty of mod_rewrite that I just couldn't seem to master. So keep that mod_rewrite reference bookmarked and you will be able to figure out any RewriteRule or RewriteCond, an amazing feat considering it took me a LONG time to figure this stuff out on my own. RewriteRule ! Require the www ^ Tags ^

10+ Mod_Rewrite Rules You Should Know Aug 18 2009 Mod_rewrite is an Apache-based rewrite engine for dynamically rewriting URLs. It’s built into Apache servers natively, though not enabled by default. It’s capable of functions beyond simple rewrites, though, some of which are included below. Turn Mod_Rewrite On Mod_rewrite is used through your .htaccess file. RewriteEngine on (Don’t forget that .htaccess commands are case-sensitive.) The Basic Mod_Rewrite Layout The basic format for a mod_rewrite command is: RewriteRule Pattern Substitution [Flag(s)] URLs are Always Relative The URL you redirect to is always relative to the directory in which your .htaccess file is placed. A Basic Redirect If you just want to create a simple 301 redirect from one URL to another, then use the following code: RewriteRule ^fileone.html$ filetwo.html This is a very basic rule that means any requests for fileone.html will be sent to filetwo.html. Require no “www” RewriteCond %{HTTP_HOST} ! Block a Specific IP Address Block Specific User Agents Prevent Hotlinking

Related: