VirtualHost中的Apache RewriteRule,用于目录重定向,不使用尾部斜杠

时间:2012-05-06 01:31:49

标签: mod-rewrite redirect virtualhost slash

如果没有尾部斜杠,我所拥有的重写网址不会重定向。 我想将访问此处“http://mysite.com/po”的用户重定向到此处:http://mysite.com/po/index_mi.php 我试过这些线但没有一个工作:

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

这是完整的虚拟主机:

<VirtualHost 192.228.100.142:80>
ServerAdmin serveradmin@mysite.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
          DirrctoryIndex 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://myothersite.com/po/index_mat.php [R=301,L]
# end Rewrite Rules #################

谢谢

===================

&LT;&LT;更新&gt;&gt; 我得到了这条线:

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

2 个答案:

答案 0 :(得分:4)

尝试:

RewriteOptions AllowNoSlash

来自mod_rewrite文档:

AllowNoSlash

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

当DirectorySlash指令设置为off时,可以启用AllowNoSlash选项以确保不再忽略重写规则。如果需要,此选项可以在.htaccess文件中应用重写规则,该文件与没有尾部斜杠的目录匹配。可在Apache HTTP Server 2.4.0及更高版本中使用。

答案 1 :(得分:0)

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

您似乎必须重新启动Apache或至少重新启动才能生效。