使用相对路径重定向的 Mod_Rewrite

问题描述:

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

I have this rule in an .htaccess file located in a directory named clips/:

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

What I am intending is that when someone visits http://example.local/clips/mlk they are redirected to http://example.local/clips/segment/index.php?clip=1

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

What actually happens is that when someone visits example.local/clips/mlk they are redirected to example.local/var/www/example/clips/segment/index.php?clip=1

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

I'm not sure why it's doing this. If I change the rewrite rule to this:

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

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

The user is redirected to example.local/segment/index.php?clip=1, which is still incorrect. I don't want to have to specify an absolute path in the case of these files being moved around the website's directory tree. How can I get this to work relatively instead of absolutely?

尝试添加如下的 RewriteBase 指令

Try adding a RewriteBase directive as below

RewriteEngine On
RewriteBase /clips/

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

编辑

但是有什么方法可以在不使用 RewriteBase 指令的情况下使其工作

but is there any way to get this to work without using a RewriteBase directive

你也可以试试

RewriteEngine On


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