使用相对路径重定向的 Mod_Rewrite [英] Mod_Rewrite with Relative Path Redirects

查看:25
本文介绍了使用相对路径重定向的 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]

这篇关于使用相对路径重定向的 Mod_Rewrite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆