Formatting code for ApacheRewrite


show source only

{{parent page="Apache"}}

===Apache Rewrite===
Rewrite should not be encouraged. Most of the time this can be handled by the application - where they have the luxury to compile and debug their work. Use rewrite to redirect to a different port or a maintenance page is fine. Using it to fix mindless applications is sickening.


%%(apache;ssl_redirect)
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/phpmyadmin(.*) https://%{SERVER_NAME}/phpmyadmin$1 [R,L]
%%

This is a stupid one requested by the client. But the QUERY_STRING part is good.
%%(apache; query_string)
RewriteEngine On
RewriteOptions MaxRedirects=10
RewriteLogLevel 2
RewriteLog /home/sites/logs/rewrite.log

RewriteCond %{REQUEST_URI} ^/list.php
RewriteCond %{QUERY_STRING} ^cat=100011$
RewriteRule .* http://www.xxx.com/cat/whatever.html [R=301,L]

RewriteCond %{QUERY_STRING} ^cat=100011(.+)$
RewriteRule .* http://www.xxx.com/cat/whatever.html%1 [R=301,L]
%%

%%(apache;host header)
RewriteCond %{HTTP_HOST} ^[^.]+\.some\.corp$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^([^.]+)\.some\.corp/?(.*) http://www.some.corp/bla/$1/$2 [R,L]
%%


Note: depending on where you put these rewrite directives, the condition may be slight different. If you are putting them in httpd.conf, the prevailing slash is necessary
%%
RewriteCond %{REQUEST_URI} ^/list.php
%%

If you are putting them into .htaccess, the prevailing slash must be ignored.
%%
RewriteCond %{REQUEST_URI} ^list.php
%%

Multiple match in request_uri
%%
RewriteCond %{REQUEST_URI} ^/educators/blog
RewriteCond %{REQUEST_URI} !^/educators/blog/index.php
RewriteCond %{QUERY_STRING} ^(cat=|tag=|p=)
RewriteRule .* /educators/blog/index.php [R=301,L]
%%

With or without www.
%%
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
%%
Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by WikkaWiki