2012-04-16 155 views
9

我想在虛擬主機配置中添加mod_rewrite規則,但它不工作。 對於「mysite.com」網站,我想將「/ webmedia /」重定向到主頁。虛擬主機配置中的mod_rewrite

以下是我有:

<VirtualHost 192.168.100.142:80> 
    ServerAdmin [email protected] 
    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> 
+0

爲什麼不將規則移動到'.htaccess'文件?這將更容易管理,因爲您不必在每次修改後重新啓動Apache。 – 2012-04-16 14:01:20

+2

.htaccess文件越來越大,我聽說服務器從vhosts文件中讀取它會更有效率。那是對的嗎? – EricP 2012-04-16 14:03:10

+2

是的,Apache可能_faster_沒有'AllowOverride All',因爲它不會在每個請求中讀取您的.htaccess文件。我猜這裏的表現收益很小。你現在在'.htaccess'中有什麼大的? – 2012-04-16 14:06:23

回答

11

,如果您有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 [email protected] 
    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> 
+0

謝謝Seybsen。就是這樣。 – EricP 2012-04-16 19:43:16

+0

如果在.htaccess文件中有更少的重寫並且在vhost配置中有更多的重寫,我在服務器上更容易嗎?或者沒有關係? – EricP 2012-04-17 14:32:44

+1

看看這個答案:http://stackoverflow.com/a/9555416/982002 – Seybsen 2012-04-17 15:01:03