Fresh .htaccess Examples: Cookies, Variables, Custom HeadersMarch 29th, 2008
« AskApache Password Protection, For WordPressApache SSL in htaccess examples »
Got some fresh .htaccess and mod_rewrite code for you! Check out the Cookie Manipulation and Tests using mod_rewrite! Also see how to set environment variables and then use them to send custom Headers (like content-language) and Rewrite based on them. And a couple Mod_Security .htaccess examples, for those smart enough to run on DreamHost. Enjoy!
.html/ to .html
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.+)\.html/\ HTTP/
RewriteRule ^(.+)\.html/$ http://www.askapache.com/$1.html [R=301,L]
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^[^\.]+\.html/$
RewriteRule ^(.*)\.html.*$ http://www.askapache.com/$1.html [R=301,L]
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(feed|wp-atom|wp-feed|wp-rss|wp-rdf|wp-commentsrss)(.*)\ HTTP/ [NC,OR]
RewriteCond %{QUERY_STRING} ^feed [NC]
RewriteCond %{HTTP_USER_AGENT} !^(FeedBurner|FeedValidator|talkr) [NC]
RewriteRule .* http://feeds.askapache.com/apache/htaccess? [R=307,L]
This code sends the Set-Cookie header to create a cookie on the client with the value of a matching item in 2nd parantheses.
RewriteEngine On RewriteBase / RewriteRule ^(.*)(de|es|fr|it|ja|ru|en)/$ - [co=lang:$2:.askapache.com:7200:/]
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} lang=([^;]+) [NC]
RewriteRule ^(.*)$ /$1?cookie-value=%1 [R,QSA,L]
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} lang=([^;]+) [NC]
RewriteRule ^(.*)$ /$1?lang=%1 [NC,L,QSA]
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE}!^.*cookie-name.*$ [NC]
RewriteRule .* /login-error/set-cookie-first.cgi [NC,L]
Accept-Language Header
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:Accept-Language} ^.*(de|es|fr|it|ja|ru|en).*$ [NC]
RewriteRule ^(.*)$ - [env=lang:%1]
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.+)/(de|es|fr|it|ja|ru|en)/\ HTTP/ [NC]
RewriteRule ^(.*)$ - [env=lang:%2]
Content-Language Header based on environment variable
Header set Content-Language "%{lang}e" env=lang
Header set Set-Cookie "language=%{lang}e; path=/;" env=lang
Header echo ^.*
ModSecurity is an open source intrusion detection and prevention engine for web applications (or a web application firewall). Operating as an Apache Web server module or standalone, the purpose of ModSecurity is to increase web application security, protecting web applications from known and unknown attacks.
<IfModule mod_security.c> SecFilterEngine Off SecFilterScanPOST Off </IfModule>
<IfModule mod_security.c> # Reject requests with status 500 SecFilterDefaultAction "deny,log,status:500" </IfModule>
« AskApache Password Protection, For WordPressApache SSL in htaccess examples »
Tags: Cookies, Headers, htaccess, mod_rewrite
The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect. Tim Berners-Lee
It's very simple -
you read the protocol
and write the code.
-Bill Joy
HTML | DCMI | GRDDL | XOXO | XDMP | XFN | DOM | XML | XHTML 1.1 Strict | CSS 2.1 | W3C | WAI | DISA | ICSI | GIAC | SANS RR | GHOST
Authority: 110 ↑ TOPExcept where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed according to these terms. "Apache" is a trademark of The ASF.
Excelentes ejemplos, me ha quedado muy claro el manejo de las cookies.
Is it possible to set the environment to the value of the URI in the current url?
In other words save the URI until the next visitor arrives.
Using htaccess…
I was looking for .htaccess examples like these… thank you!
Yep very nice Cookie Manipulation. Hope you will write more topics like URL Hotlink Protections and Vertiual Directory Creations
Wow awesome cookie examples!