.htaccess 重定向到子目录中的脚本

发布于 2024-07-13 05:50:44 字数 514 浏览 7 评论 0原文

我需要有一个 .htaccess 重写规则重定向到子目录根目录中的脚本,但仅限于该子目录中的任何文件/目录...

/
/subdir/
   /index.php
   /somedir/
   /anotherdir/

对于上面的结构,我希望浏览到 /subdir 的任何人都转到 / subdir/index.php,以及任何去 /subdir/somedir/ 到 /subdir/index.php 等的人...

但是,如果他们去根目录。 他们应该留在那里......我已经尝试将 .htaccess 文件放入 /subdir 中,并尝试了一系列重写规则,但到目前为止没有任何效果。

更新:我并不是说只有/somedir/需要重定向到index.php,而是/subdir/的任何子目录都需要重定向被重定向...脚本也是如此,但显然不是 /subdir/index.php

抱歉,我之前不清楚。

I need to have a .htaccess rewrite rule redirect to a script in the root of a subdirectory, but only for any files/directories in that sub directory...

/
/subdir/
   /index.php
   /somedir/
   /anotherdir/

For the structure above, I want anyone browsing to /subdir to go to /subdir/index.php, and anyone going to /subdir/somedir/ to /subdir/index.php etc...

If however, they go to the root directory. they should stay there... I've tried putting an .htaccess file in /subdir with a bunch of attempts at rewrite rules but so far none work.

update: I don't mean just the /somedir/ needs to redirect ot index.php, but any subdirectory of /subdir/ needs to be redirect... Scripts too, but obviously not /subdir/index.php

Sorry I wasn't clear before.

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

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

发布评论

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

评论(2

天赋异禀 2024-07-20 05:50:45

如果我理解你的问题,唯一需要发生的实际重定向是如果有人转到 /subdir/somedir (将它们重定向回 index.php

如果是这种情况,这应该可以工作,放入 /.htaccess:

redirect /subdir/somedir/ /subdir/

If I'm understanding your question, the only actual redirection that needs to happen is if someone goes to /subdir/somedir (redirect them back to the index.php

If that's the case, this should work, put in /.htaccess:

redirect /subdir/somedir/ /subdir/
没︽人懂的悲伤 2024-07-20 05:50:45

如果您只想要内部重定向,请尝试这个 mod_rewrite 示例

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/subdir/somedir/index\.php
RewriteRule ^subdir/somedir/ subdir/index.php [L]

:外部重定向,只需添加 R 标志(带有可选的重定向状态代码:R=xxx;默认:302):

RewriteRule … [L,R=xxx]

If you just want an internal redirect, try this mod_rewrite example:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/subdir/somedir/index\.php
RewriteRule ^subdir/somedir/ subdir/index.php [L]

For an external redirect, just add the R flag (with an optional redirect status code: R=xxx; default: 302):

RewriteRule … [L,R=xxx]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文