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

查看:371
本文介绍了mod_rewrite的使用相对路径重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在位于名为目录中的.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 /短片/段/的index.php?夹= 1

什么确实的情况是,当有人访问 example.local /短片/ MLK ,他们将被重定向到的例子。本地的/ var / WWW /例子/短片/段/的index.php?夹= 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 /段/的index.php?夹= 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天全站免登陆