From: Rich Bowen mod_rewri
request. This includes many examples of common uses of mod_rewrite,
including detailed descriptions of how each works.
Note that many of these examples won't work unchanged in your +
From Old to New (internal)
Rewriting From Old to New (external)
From Static to Dynamic
Canonical HostnamesAssume we have recently renamed the page
+ foo.html to bar.html and now want
+ to provide the old URL for backward compatibility. However,
+ we want that users of the old URL even not recognize that
+ the pages was renamed - that is, we don't want the address to
+ change in their browser.
We rewrite the old URL to the new one internally via the + following rule:
+ ++RewriteEngine on +RewriteRule ^/old\.html$ /new.html [PT] +
Assume again that we have recently renamed the page
+ foo.html to bar.html and now want
+ to provide the old URL for backward compatibility. But this
+ time we want that the users of the old URL get hinted to
+ the new one, i.e. their browsers Location field should
+ change, too.
We force a HTTP redirect to the new URL which leads to a + change of the browsers and thus the users view:
+ ++RewriteEngine on +RewriteRule ^/foo\.html$ bar.html [R] +
In this example, as contrasted to the internal example above, we can simply + use the Redirect directive. mod_rewrite was used in that earlier + example in order to hide the redirect from the client:
+ +
+ Redirect /foo.html /bar.html
+
How can we transform a static page
+ foo.html into a dynamic variant
+ foo.cgi in a seamless way, i.e. without notice
+ by the browser/user.
We just rewrite the URL to the CGI-script and force the
+ handler to be cgi-script so that it is
+ executed as a CGI program.
+ This way a request to /~quux/foo.html
+ internally leads to the invocation of
+ /~quux/foo.cgi.
+RewriteEngine on +RewriteBase /~quux/ +RewriteRule ^foo\.html$ foo.cgi [H=cgi-script] +
For sites running on a port other than 80:
+
+RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
+RewriteCond %{HTTP_HOST} !^$
+RewriteCond %{SERVER_PORT} !^80$
+RewriteRule ^/?(.*) http://www.example.com:%{SERVER_PORT}/$1 [L,R,NE]
+And for a site running on port 80
+
+RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
+RewriteCond %{HTTP_HOST} !^$
+RewriteRule ^/?(.*) http://www.example.com/$1 [L,R,NE]
++ If you wanted to do this generically for all domain names - that + is, if you want to redirect example.com to + www.example.com for all possible values of + example.com, you could use the following + recipe:
+ +
+RewriteCond %{HTTP_HOST} !^www\. [NC]
+RewriteCond %{HTTP_HOST} !^$
+RewriteRule ^/?(.*) http://www.%{HTTP_HOST}/$1 [L,R,NE]
+These rulesets will work either in your main server configuration
+ file, or in a .htaccess file placed in the DocumentRoot of the server.
Available Languages: en
Canonical URLs
Canonical Hostnames
Moved DocumentRoot
Trailing Slash Problem
Move Homedirs to Different Webserver
Redirecting Anchors
Time-Dependent Rewriting
Backward Compatibility for YYYY to XXXX migration
From Old to New (intern)
From Old to New (extern)
From Static to Dynamic
Proxy Deny
External Rewriting Engine
Web Cluster with Consistent URL SpaceFor sites running on a port other than 80:
-
-RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
-RewriteCond %{HTTP_HOST} !^$
-RewriteCond %{SERVER_PORT} !^80$
-RewriteRule ^/?(.*) http://www.example.com:%{SERVER_PORT}/$1 [L,R,NE]
-And for a site running on port 80
-
-RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
-RewriteCond %{HTTP_HOST} !^$
-RewriteRule ^/?(.*) http://www.example.com/$1 [L,R,NE]
-- If you wanted to do this generically for all domain names - that - is, if you want to redirect example.com to - www.example.com for all possible values of - example.com, you could use the following - recipe:
- -
-RewriteCond %{HTTP_HOST} !^www\. [NC]
-RewriteCond %{HTTP_HOST} !^$
-RewriteRule ^/?(.*) http://www.%{HTTP_HOST}/$1 [L,R,NE]
-These rulesets will work either in your main server configuration
- file, or in a .htaccess file placed in the DocumentRoot of the server.
DocumentRootAssume we have recently renamed the page
- foo.html to bar.html and now want
- to provide the old URL for backward compatibility. Actually
- we want that users of the old URL even not recognize that
- the pages was renamed.
We rewrite the old URL to the new one internally via the - following rule:
- --RewriteEngine on -RewriteBase /~quux/ -RewriteRule ^foo\.html$ bar.html -
Assume again that we have recently renamed the page
- foo.html to bar.html and now want
- to provide the old URL for backward compatibility. But this
- time we want that the users of the old URL get hinted to
- the new one, i.e. their browsers Location field should
- change, too.
We force a HTTP redirect to the new URL which leads to a - change of the browsers and thus the users view:
- --RewriteEngine on -RewriteBase /~quux/ -RewriteRule ^foo\.html$ bar.html [R] -
How can we transform a static page
- foo.html into a dynamic variant
- foo.cgi in a seamless way, i.e. without notice
- by the browser/user.
We just rewrite the URL to the CGI-script and force the
- handler to be cgi-script so that it is
- executed as a CGI program.
- This way a request to /~quux/foo.html
- internally leads to the invocation of
- /~quux/foo.cgi.
-RewriteEngine on -RewriteBase /~quux/ -RewriteRule ^foo\.html$ foo.cgi [H=cgi-script] -
For sites running on a port other than 80:
-
-RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
-RewriteCond %{HTTP_HOST} !^$
-RewriteCond %{SERVER_PORT} !^80$
-RewriteRule ^/?(.*) http://www.example.com:%{SERVER_PORT}/$1 [L,R,NE]
-And for a site running on port 80
-
-RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
-RewriteCond %{HTTP_HOST} !^$
-RewriteRule ^/?(.*) http://www.example.com/$1 [L,R,NE]
-- If you wanted to do this generically for all domain names - that - is, if you want to redirect example.com to - www.example.com for all possible values of - example.com, you could use the following - recipe:
- -
-RewriteCond %{HTTP_HOST} !^www\. [NC]
-RewriteCond %{HTTP_HOST} !^$
-RewriteRule ^/?(.*) http://www.%{HTTP_HOST}/$1 [L,R,NE]
-These rulesets will work either in your main server configuration
- file, or in a .htaccess file placed in the
DocumentRootAssume we have recently renamed the page
- foo.html to bar.html and now want
- to provide the old URL for backward compatibility. Actually
- we want that users of the old URL even not recognize that
- the pages was renamed.
We rewrite the old URL to the new one internally via the - following rule:
- --RewriteEngine on -RewriteBase /~quux/ -RewriteRule ^foo\.html$ bar.html -
Assume again that we have recently renamed the page
- foo.html to bar.html and now want
- to provide the old URL for backward compatibility. But this
- time we want that the users of the old URL get hinted to
- the new one, i.e. their browsers Location field should
- change, too.
We force a HTTP redirect to the new URL which leads to a - change of the browsers and thus the users view:
- --RewriteEngine on -RewriteBase /~quux/ -RewriteRule ^foo\.html$ bar.html [R] -
How can we transform a static page
- foo.html into a dynamic variant
- foo.cgi in a seamless way, i.e. without notice
- by the browser/user.
We just rewrite the URL to the CGI-script and force the
- handler to be cgi-script so that it is
- executed as a CGI program.
- This way a request to /~quux/foo.html
- internally leads to the invocation of
- /~quux/foo.cgi.
-RewriteEngine on -RewriteBase /~quux/ -RewriteRule ^foo\.html$ foo.cgi [H=cgi-script] -