10

I'm trying to add mod_rewrite rules in the vhost config but it's not working. For the site "mysite.com" I want to redirect "/webmedia/" to the home page .

Here is what I have:

<VirtualHost 192.168.100.142:80>
    ServerAdmin serveradmin@bbgi.com
    DocumentRoot /home/drupal_1
    ServerName mysite.com
    ServerAlias www.mysite.com
    Alias /movies /home/movies/
    ErrorLog /var/log/httpd/mysite.com_err_log
      CustomLog /var/log/httpd/mysite.com_log special
    <Directory /home/drupal_1>
      Options FollowSymLinks Includes ExecCGI
              AllowOverride All
              DirectoryIndex index.html index.htm index.php

      # Rewrite Rules #####################
      RewriteEngine On
      RewriteRule ^/webmedia/(.*) / [R=301,L]
      # end Rewrite Rules #################

    </Directory>
    <Directory /home/movies>
      Options FollowSymLinks Includes ExecCGI
              AllowOverride All
              DirectoryIndex index.html index.htm index.php
    </Directory>

</VirtualHost>
Kara
  • 6,115
  • 16
  • 50
  • 57
EricP
  • 1,459
  • 6
  • 33
  • 55
  • Why not moving your rules to an `.htaccess` file ? It will be much more easier to manage since you won't have to restart Apache after every modification you make. – Pierre-Olivier Apr 16 '12 at 14:01
  • 2
    The .htaccess file is getting large and I heard that it's more efficient for the server to read it from the vhosts file. Is that correct? – EricP Apr 16 '12 at 14:03
  • 2
    Yes, Apache is probably _faster_ without `AllowOverride All` because it won't have to read your .htaccess file on every request. I guess the gain of performance is _very_ small here. What do you currently have in your `.htaccess` for it to be large ? – Pierre-Olivier Apr 16 '12 at 14:06
  • 1
    It's in a multi-site Drupal installation which has many items for Drupal to work but I have to add many rewrites to prevent many "page not found" errors being logged in the database. We moved from another server and there are so many old pages and directories that are trying to be reached that don't exist anymore. Also I'd just like to know how how for my own knowledge. thanks. – EricP Apr 16 '12 at 14:14

2 Answers2

14

This should work if you have mod_rewrite loaded.

<Directory /home/drupal_1>
    Options FollowSymLinks Includes ExecCGI
    AllowOverride All
    DirectoryIndex index.html index.htm index.php
</Directory>
<Directory /home/movies>
    Options FollowSymLinks Includes ExecCGI
    AllowOverride All
    DirectoryIndex index.html index.htm index.php
</Directory>
<VirtualHost 192.168.100.142:80>
    ServerAdmin serveradmin@bbgi.com
    DocumentRoot /home/drupal_1
    ServerName mysite.com
    ServerAlias www.mysite.com
    Alias /movies /home/movies/
    ErrorLog /var/log/httpd/mysite.com_err_log
    CustomLog /var/log/httpd/mysite.com_log special

    # Rewrite Rules #####################
    RewriteEngine On
    RewriteRule ^/webmedia/(.*) / [R=301,L]
    # end Rewrite Rules #################   
</VirtualHost>
Seybsen
  • 14,989
  • 4
  • 40
  • 73
1
<Directory /home/drupal_1>
  Options FollowSymLinks Includes ExecCGI
          AllowOverride All
          DirectoryIndex index.html index.htm index.php

  # Rewrite Rules #####################
  RewriteEngine On
  RewriteRule ^/webmedia/(.*) / [R=301,L]
  # end Rewrite Rules #################
</Directory>

This RewriteRule pattern would never match in a directory context (ie. inside a <Directory> container) because of the slash prefix. It would have needed to have been written like this:

RewriteRule ^webmedia/ / [R=301,L]

(The trailing (.*) was superfluous.)

However, since it's in a <Directory> container, any mod_rewrite directives that you have in .htaccess (since you have AllowOverride All) could potentially override this.

If you are using .htaccess and this is undesirable then probably better to take it out of the <Directory> container and have it directly in the <VirtualHost> container (a virtualhost context) - as @Seybsen has done in his answer.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • This worked for me. I had success when adding the rewrite rules within the Directory directive of the virtual host along with `Options +FollowSymLinks +SymLinksIfOwnerMatch` – Jimbo Mar 08 '23 at 17:51
  • 1
    @Jimbo You shouldn't need to set both `FollowSymLinks` and `SymLinksIfOwnerMatch`. The later is a subset of the former. (I would expect the later to override the former.) – MrWhite Mar 08 '23 at 19:20