Подтвердить что ты не робот

Mod_rewrite в конфигурации vhosts

Я пытаюсь добавить правила mod_rewrite в конфигурацию vhost, но он не работает. Для сайта "mysite.com" я хочу перенаправить "/webmedia/" на домашнюю страницу.

Вот что у меня есть:

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

Ответ 1

Это должно работать, если вы загрузили 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>

Ответ 2

<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> (контекст virtualhost) - как это сделал @Seybsen в своем ответе.