vhosts配置中的mod_rewrite

时间:2012-04-16 13:55:58

标签: apache mod-rewrite virtualhost

我正在尝试在vhost配置中添加mod_rewrite规则,但它无法正常工作。 对于网站“mysite.com”,我想将“/ webmedia /”重定向到主页。

这就是我所拥有的:

<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>

2 个答案:

答案 0 :(得分:14)

如果你加载了mod_rewrite,这应该有效。

<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>

答案 1 :(得分:0)

<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>

由于斜杠前缀,此RewriteRule 模式目录上下文中(即在<Directory>容器中)将永远不匹配。可能需要这样写:

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

(结尾的(.*)是多余的。)

但是,由于它位于<Directory>容器中,因此您在.htaccess中拥有的所有mod_rewrite伪指令(因为您已经拥有AllowOverride All)都可能会覆盖它。

如果您使用的是.htaccess,那么最好将其从<Directory>容器中取出,然后将其直接放在<VirtualHost>容器中(virtualhost context )-就像@Seybsen在回答中所做的那样。