带相对路径重定向的 Mod_Rewrite

发布于 2025-01-01 11:34:33 字数 654 浏览 4 评论 0原文

我在名为 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,这仍然不正确。当这些文件在网站的目录树中移动时,我不想指定绝对路径。我怎样才能让它相对而不是绝对地工作?

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]

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

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]

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许你一世情深 2025-01-08 11:34:33

尝试添加如下 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]

Try adding a RewriteBase directive as below

RewriteEngine On
RewriteBase /clips/

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

EDIT

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

You could also try

RewriteEngine On


RewriteCond %{REQUEST_URI} ^(/[^/]+/) [NC] 
RewriteRule ^mlk/?$ http://%{HTTP_HOST}%1segment/index.php?clip=1 [R=301,QSA,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文