【问题标题】:Apache RewriteRule in VirtualHost for directory redirect not working without trailing slashVirtualHost中用于目录重定向的Apache RewriteRule在没有斜杠的情况下不起作用
【发布时间】:2012-05-15 02:15:22
【问题描述】:

如果我的重写 url 没有尾部斜杠,则它不会重定向。 我想将到这里http://example.com/po 的用户重定向到这里:http://new.example.com/po/index_mi.php

我尝试了这些行,但没有一个起作用:

RewriteRule  /po(/)?$ http://new.example.com/po/index_mat.php [R=301,L]
RewriteRule ^/po(/)$ http://new.example.com/po/index_mat.php [R=301,L]
RewriteRule ^/po$ http://new.example.com/po/index_mat.php [R=301,L]

这是完整的虚拟主机:

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

# Rewrite Rules #####################
RewriteEngine On
RewriteRule ^/webmedia/(.*) / [R=301,L]
RewriteRule ^/marquee.php / [R=301,L]
RewriteRule ^/register /user/register [R=301,L]
RewriteRule  ^/po(/)?$ http://new.example.com/po/index_mat.php [R=301,L]
# end Rewrite Rules #################

</VirtualHost>

更新:

我得到了它与这条线的合作:

RewriteRule ^/po$ http://new.example.com/po/index_mat.php [R=301,L]

【问题讨论】:

    标签: mod-rewrite redirect virtualhost slash


    【解决方案1】:

    试试:

    RewriteOptions AllowNoSlash
    

    来自mod_rewrite documentation

    AllowNoSlash

    默认情况下,mod_rewrite 将忽略映射到 磁盘但缺少尾部斜杠,期望 mod_dir 模块将向客户端发出重定向到规范 URL 一个斜杠。

    DirectorySlash 指令设置为off 时,AllowNoSlash 可以启用选项以确保不再重写规则 忽略。此选项可以在内部应用重写规则 .htaccess 匹配目录的文件,没有尾部斜杠,如果 如此渴望。

    在 Apache HTTP Server 2.4.0 及更高版本中可用。

    【讨论】:

    • 我不确定该行在 vhost conf 文件中的位置。我试着把它放在“RewriteEngine On”下,但没有用。谢谢
    • 当我在“RewriteEngine On”下添加该行并重新启动 Apache 时,它​​给了我一个错误 RewriteOptions: unknown option 'AllowNoSlash'。好消息是,我现在可以在没有它的情况下使用它。我正在使用这个: RewriteRule ^/po$ myothersite.com/po/index_mat.php [R=301,L] 我不知道为什么它现在而不是以前工作,也许 Apache 没有正确重启?
    【解决方案2】:
    RewriteRule ^/po$ http://new.example.com/po/index_mat.php [R=301,L]
    

    看来你必须重启 Apache 或者至少做一个优雅重启才能生效。

    【讨论】:

      猜你喜欢
      • 2014-09-29
      • 2015-09-20
      • 1970-01-01
      • 1970-01-01
      • 2016-12-06
      • 2013-09-08
      • 1970-01-01
      • 2014-11-21
      • 1970-01-01
      相关资源
      最近更新 更多