I am using apache2 server running on debian which forwards all the http request to tomcat installed on same machine.

I have two files under my /etc/apache2/ folder

apache2.conf and httpd.conf

I modified httpd.conf file to look like following.

# forward all http request on port 80 to tomcat
ProxyPass / ajp://
ProxyPassReverse / ajp://

# gzip text content
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
DeflateCompressionLevel 9
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

# Turn on Expires and mark all static content to expire in a week
# unset last modified and ETag 
ExpiresActive On
ExpiresDefault A0
<FilesMatch "\.(jpg|jpeg|png|gif|js|css|ico)$">
 ExpiresDefault A604800
 Header unset Last-Modified
 Header unset ETag
 FileETag None
 Header append Cache-Control "max-age=604800, public"

RewriteEngine On

# rewrite all www.example.com/content/XXX-01.js and YYY-01.css files to XXX.js and YYY.css
RewriteRule ^content/(js|css)/([a-z]+)-([0-9]+)\.(js|css)$ /content/$1/$2.$4

# remove all query parameters from URL after we are done with it
RewriteCond %{THE_REQUEST} ^GET\ /.*\;.*\ HTTP/
RewriteCond %{QUERY_STRING} !^$
RewriteRule .* http://example.com%{REQUEST_URI}? [R=301,L]

# rewrite all www.example.com to example.com
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

I want to achieve following.

  1. forward all traffic to tomcat
  2. GZIP all the text content.
  3. Put 1 week expiry header to all static files and unset ETag/last modified header.
  4. rewrite all js and css file to certain format.
  5. remove all the query parameters from URL
  6. forward all www.example.com to example.com

The problem is only 1 and 2 are working. I tried a lot with many combinations but the expire and rewrite rule (3-6) do not work at all. I also tried moving these rules to apache2.conf and .htaccess files but it didn't work either. It does not give any error but these rules are simple ignored.

expires and rewrite modules are ENABLED.

Please let me know what should I do to fix this. 1. Do I need to add something else in httpd.conf file (like Options +FollowSymLink) or something else? 2. Do I need to add something in apache2.conf file? 3. Do I need to move these rules to .htaccess file? If yes, what should I write in that file and where should I keep that file? in /etc/apache2/ folder or /var/www/ folder? 4. Any other info to make this work?

Thanks, Ankit

Probably the ProxyPass / directive you have in your config routes all requests to the Tomcat backend, bypassing all your mod_rewrite directives. You can try to remove the ProxyPass directive and use RewriteRule with the [P] flag after your other rewrites:

For the expire headers the situation is worse — in another similar question a solution was not found.

Option +FollowSymLinks would be needed for using mod_rewrite in .htaccess files; in your case it is not required, because you can put everything in the Apache config. Moving rules from config to .htaccess is pointless and can only make things slower. However, there is an important difference between .htaccess and the Apache config — in .htaccess patterns in RewriteRule are matched against relative filesystem paths (which never start with /), while in the VirtualHost context these patterns are matched against the URL path after the hostname, which always starts with /. Therefore at least one of your rules is incorrect:

RewriteRule ^content/(js|css)/([a-z]+)-([0-9]+)\.(js|css)$ /content/$1/$2.$4

should be

RewriteRule ^/content/(js|css)/([a-z]+)-([0-9]+)\.(js|css)$ /content/$1/$2.$4

(note the additional slash in the beginning).

imo you should change the rules as following:

#is the path correct? your comment stats something else
RewriteRule ^/content/(js|css)/([a-z]+)-([0-9]+)\.(js|css) /content/$1/$2.$4

# rewrite all www.example.com to example.com
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^/(.*) http://example.com/$1 [R=301,L]

to get more info, take a look at this directive and the following. after enabling logging try it all again and look at the logs.

have you checked whether case of your regex and the case of your files match?

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.