通过 2 个 url 访问的 .htaccess 内容始终重定向到第一个 url

发布于 2024-12-28 21:16:08 字数 1942 浏览 1 评论 0原文

所以我有一个奇怪的服务器设置,我对此没有太多控制权。我的生产站点如下所示:

/www/users/name    <- server points here
    /public    <- I want it to point here, but I don't have that option
        index.php    <- all requests should eventually end up here

可以通过 www.test.com/users/namewww.test.com/~name 访问

我的开发站点如下所示:

/www/name/public    <- dev server points here
    index.php    <- all requests should eventually end up here

只能通过 www.test.dev 访问

我正在使用 PHP 框架,它强制我的 url 始终具有相同的基数才能工作(Laravel)。所以 www.test.com/~name/stuff 可以工作,但 www.test.com/users/name/stuff 不起作用。我希望我的网址始终显示为前者,以解决此问题。

我为我的生产基目录创建了一个 .htaccess 文件,该文件将始终转发到公共目录,如下所示:

    RewriteEngine on

    RewriteRule ^(.*)$ public/$1

公共目录有一个像这样的 .htaccess 文件,它随我正在使用的框架一起提供(我不想更改它,因为我的开发网站工作得很好):

    RewriteEngine on

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule ^(.*)$ index.php/$1 [L]

这样,所有 /~name 请求都可以完美工作,但 /users/name 都不起作用。 如何修复我的生产基目录 .htaccess 文件以强制从 /users/name 到 /~name 的所有请求?

我尝试添加:

RedirectMatch ^/users/name/(.*)$ /~name/$1

这不起作用,因为重写已被显示。例如,如果我请求 www.test.com/~name/stuff,则会显示为 www.test.com/~name/public/index.php/stuff >。如果没有 RedirectMatch 行,所有 ~name 请求都可以正常工作。

奇怪的是,如果我将上面的规则交换为:

RedirectMatch ^/~name/(.*)$ /users/name/$1

一切都很完美,除了我的网址总是有 /users/name 而不是我喜欢的 /~name

显然,如果我所要求的不可能实现,我愿意接受这一点。如果有人可以解释为什么上面的方法有效,但前一个方法不起作用,那就太好了。我确实发现我可以通过将 RedirectBase /~name 添加到两个 .htaccess 文件来使第一个工作,但正如我上面提到的,我不想更改公共 .htaccess 文件,因为这两个文件我的开发和生产环境使用它。

提前致谢!

So I have a strange server set-up which I don't have a lot of control over. My production site looks like this:

/www/users/name    <- server points here
    /public    <- I want it to point here, but I don't have that option
        index.php    <- all requests should eventually end up here

This can be accessed via www.test.com/users/name AND www.test.com/~name

My dev site looks like this:

/www/name/public    <- dev server points here
    index.php    <- all requests should eventually end up here

This is only accessed via www.test.dev

I'm using a PHP Framework which forces my urls to always have the same base in order to work (Laravel). So www.test.com/~name/stuff will work, but www.test.com/users/name/stuff will NOT work. I would like my urls to always appear as the former to get around this issue.

I created a .htaccess file for my production base directory which will always forward to the public directory like this:

    RewriteEngine on

    RewriteRule ^(.*)$ public/$1

The public directory has a .htaccess file like this which came with the framework I'm using (which I would like to not change since my dev site works just fine with it):

    RewriteEngine on

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule ^(.*)$ index.php/$1 [L]

With just this, all /~name requests work perfectly, but none of the /users/name work. How do I fix my production base directory .htaccess file to force all requests from /users/name to /~name?

I have tried adding:

RedirectMatch ^/users/name/(.*)$ /~name/$1

Which does NOT work because the rewrites are revealed. For example, if I request www.test.com/~name/stuff, this shows up as www.test.com/~name/public/index.php/stuff. Without the RedirectMatch line, all ~name requests work perfectly fine.

Curiously, if I swap the rule above to:

RedirectMatch ^/~name/(.*)$ /users/name/$1

Everything works perfect except that my urls will always have /users/name instead of /~name like I would prefer.

Obviously, I am willing to live with this if what I ask is not possible. If someone could explain why the above works but the prior one does not that would be nice too. I did find that I could get the first one to work by adding RedirectBase /~name to BOTH .htaccess files, but as I mentioned above, I would like to not change the public .htaccess file since both my dev and production environments use it.

Thanks in advance!

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

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

发布评论

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

评论(1

剪不断理还乱 2025-01-04 21:16:08

以下对我有用:

RewriteEngine on

RewriteBase /~name    

RewriteCond %{REQUEST_URI} /users/name(.*)
RewriteRule ^(.*)$ $1 [L,R]

RewriteRule ^(.*)$ public/$1

仍然不确定为什么这有效,而我上面描述的却不起作用...推测:我认为这与从 RedirectMatch 发生重定向时有关。在此版本中,我强制从 /users/name 到 /~name 的重定向始终首先发生,然后再进行任何其他重写。我相信上面的 RedirectMatch 的重定向是在其他重写之后发生的,这就是重写出现在地址栏中的原因。我还认为另一个方向的重定向效果很好,因为 /~name 很可能有一个别名到实际路径 /users/name。因此,在一种情况下必须将 /~name 替换为 /users/name,而在另一种情况下则不必。

The following works for me:

RewriteEngine on

RewriteBase /~name    

RewriteCond %{REQUEST_URI} /users/name(.*)
RewriteRule ^(.*)$ $1 [L,R]

RewriteRule ^(.*)$ public/$1

Still not sure why this works and what I described above doesn't... Speculation: I assume this has something to do with when the redirect happens from RedirectMatch. In this version, I am forcing the redirect from /users/name to /~name to always happen first, before any other rewrites happen. I believe that the redirect from RedirectMatch above was happening after the other Rewrites which was why the rewrites appeared in the address bar. I also think that the Redirect in the other direction worked fine because there is most likely an Alias for /~name to the actual path /users/name. So it has to replace /~name with /users/name in one case but not the other.

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