具有相对路径重定向的Mod_Rewrite

Ian*_*ter 4 linux ubuntu mod-rewrite redirect apache2

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

RewriteRule ^mlk/?$ segment/index.php?clip=1 [R=301,QSA,L]
Run Code Online (Sandbox Code Playgroud)

我想要的是,当有人访问时,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]
Run Code Online (Sandbox Code Playgroud)

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

Ulr*_*lha 6

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

RewriteEngine On
RewriteBase /clips/

RewriteRule ^mlk/?$ segment/index.php?clip=1 [R=301,QSA,L]
Run Code Online (Sandbox Code Playgroud)

编辑

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

你也可以试试

RewriteEngine On


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

  • RewriteBase指令有效 - 但是有没有办法让它在不使用RewriteBase指令的情况下工作? (2认同)