Forum Moderators: phranque

Message Too Old, No Replies

Where To Put Rewrite Code In httpd.conf?

It works only for some sites!

         

eggerda

5:23 am on Mar 22, 2007 (gmt 0)

10+ Year Member



Hello,

After much frustration, I finally go my rewrite rule working in the httpd.conf file.

Here is the code:

<VirtualHost 99.99.99.999>
DocumentRoot "/var/www/html/site.com"
ServerName www.site.com
ServerAlias site.com
CustomLog logs/www.site.com-access_log combined
# Begin Rewrite Code
Options +FollowSymLinks
RewriteEngine on
# if hostname is not blank
RewriteCond %{HTTP_HOST} .
# and if hostname does not start with "www."
RewriteCond %{HTTP_HOST}!^www\.
# prepend "www." to hostname and redirect
RewriteRule ^/(.*)$ [%{HTTP_HOST}...] [R=301,L]
<Directory "/var/www/html/site.com">
allow from all
Options -Indexes
</Directory>
</VirtualHost>

The reason it wasn't working before was I had it outside of the Virtual Host tags I guess.

Anyway, it's working now, but I want it to apply to EVERY site on the server instead of just the site which it resides in the VirtualHost tags.

Where should I put the rewrite code so it works for ALL virtual hosts on the server?

Thanks,

Dan

jdMorgan

3:55 pm on Mar 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As long as the "setup" directives Options and RewriteEngine precede the actual RewriteCond and RewriteRule directives, then you can use them in server config, <VirtualHost>, <Directory>, or per-directory .htaccess contexts, as documented in the mod_rewrite documentation.

Jim