具有相对路径重定向的Mod_Rewrite

时间:2012-02-01 19:56:15

标签: linux mod-rewrite redirect ubuntu apache2

我在位于名为clips/的目录中的.htaccess文件中有此规则:

RewriteRule ^mlk/?$ segment/index.php?clip=1 [R=301,QSA,L]

我打算在有人访问http://example.local/clips/mlk时将其重定向到http://example.local/clips/segment/index.php?clip=1

实际发生的事情是当有人访问example.local/clips/mlk时,他们会被重定向到example.local/var/www/example/clips/segment/index.php?clip=1

我不确定为什么会这样做。如果我将重写规则更改为:

RewriteRule ^mlk/?$ /segment/index.php?clip=1 [R=301,QSA,L]

用户被重定向到example.local / segment / index.php?clip = 1,这仍然是不正确的。我不想在这些文件在网站目录树中移动的情况下指定绝对路径。我怎样才能让它相对而不是绝对地工作?

1 个答案:

答案 0 :(得分:6)

尝试添加RewriteBase指令,如下所示

RewriteEngine On
RewriteBase /clips/

RewriteRule ^mlk/?$ segment/index.php?clip=1 [R=301,QSA,L]

修改

  

但是有没有办法让它在不使用RewriteBase指令的情况下工作

您也可以尝试

RewriteEngine On


RewriteCond %{REQUEST_URI} ^(/[^/]+/) [NC] 
RewriteRule ^mlk/?$ http://%{HTTP_HOST}%1segment/index.php?clip=1 [R=301,QSA,L]