10

我正在尝试在 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>
4

2 回答 2

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>
于 2012-04-16T18:59:27.310 回答
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>容器中,因此您拥有的任何 mod_rewrite 指令.htaccess(因为您拥有AllowOverride All)都可能会覆盖它。

如果您正在使用.htaccess并且这是不可取的,那么最好将其从<Directory>容器中取出并直接放在<VirtualHost>容器中(virtualhost 上下文) - 正如@Seybsen 在他的回答中所做的那样。

于 2018-12-17T16:11:44.123 回答