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!

Table of Contents

  1. Introduction
    1. Mod_Rewrite .htaccess Examples
      1. Redirect Request ending in .html/ to .html
      2. Or a lower quality alternative
      3. Redirect All Feeds to Feedburner's MyBrand
    2. Cookie Manipulation and Tests with mod_rewrite
      1. Set a Cookie based on Requested directory
      2. Get Cookie Value
      3. Rewrite Based on Cookie Value
      4. Redirect If Cookie Not Set
    3. Setting Environment Variables
      1. Set lang var to Accept-Language Header
      2. Set lang var to URI
    4. Using the Environment Variable
      1. Send Content-Language Header based on environment variable
      2. Set a Cookie with env variable value only if has value
      3. Echo all Headers back!
    5. Mod_Security .htaccess Examples
      1. Turn OFF mod_security
      2. Reject Requests with Status 500

  • Mod_Rewrite .htaccess Examples
  • Cookie Manipulation and Tests with mod_rewrite
  • Setting Environment Variables
  • Using the Environment Variable
  • Mod_Security .htaccess Examples

Mod_Rewrite .htaccess Examples

Redirect Request ending in .html/ to .html

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.+).html/ HTTP/
RewriteRule ^(.+).html/$ https://www.askapache.com/$1.html [R=301,L]

Or a lower quality alternative

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^[^.]+.html/$
RewriteRule ^(.*).html.*$ https://www.askapache.com/$1.html [R=301,L]

Redirect All Feeds to Feedburner's MyBrand

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]

Cookie Manipulation and Tests with mod_rewrite

Set a Cookie based on Requested directory

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:/]

Get Cookie Value

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} lang=([^;]+) [NC]
RewriteRule ^(.*)$ /$1?cookie-value=%1 [R,QSA,L]

Rewrite Based on Cookie Value

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} lang=([^;]+) [NC]
RewriteRule ^(.*)$ /$1?lang=%1 [NC,L,QSA]

Redirect If Cookie Not Set

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} !^.*cookie-name.*$ [NC]
RewriteRule .* /login-error/set-cookie-first.cgi [NC,L]

Setting Environment Variables

Set lang var to Accept-Language Header

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:Accept-Language} ^.*(de|es|fr|it|ja|ru|en).*$ [NC]
RewriteRule ^(.*)$ - [env=lang:%1]

Set lang var to URI

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.+)/(de|es|fr|it|ja|ru|en)/ HTTP/ [NC]
RewriteRule ^(.*)$ - [env=lang:%2]

Using the Environment Variable

Send Content-Language Header based on environment variable

Header set Content-Language "%{lang}e" env=lang

Set a Cookie with env variable value only if has value

Header set Set-Cookie "language=%{lang}e; path=/;" env=lang

Echo all Headers back!

Header echo ^.*

Mod_Security .htaccess Examples

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.

Turn OFF mod_security


SecFilterEngine Off
SecFilterScanPOST Off

Reject Requests with Status 500


# Reject requests with status 500
SecFilterDefaultAction "deny,log,status:500"

.htaccess Examples: Cookies, Variables, Custom Headers - AskApache

Charles Torvalds
3 Aug 2024

  • Site Map WireShark GNU Non-GNU Tor Project cURL TLDP - Documentation
  • Htaccess Files Hacking Htaccess Javascript Linux Optimization PHP Security Shell Scripting WordPress
  • Base64 Image Converter Raw HTTP Header Debugger Graphical ASCII Text Generator Mac Address Vendor Lookup Who Am I – Your IP Information Request Method Security Scanner .htpasswd file Generator Compress CSS DNS Tracer
Copyright © 2025 AskApache
  • Site Map
  • Htaccess Files
  • Hacking
  • Htaccess
  • Javascript
  • Linux
  • Optimization
  • PHP
  • Security
  • Shell Scripting
  • WordPress
  • Base64 Image Converter
  • Raw HTTP Header Debugger
  • Graphical ASCII Text Generator
  • Mac Address Vendor Lookup
  • Who Am I – Your IP Information
  • Request Method Security Scanner
  • .htpasswd file Generator
  • Compress CSS
  • DNS Tracer
Exit mobile version