帮助解决简单的 RewriteRule 问题

发布于 2024-10-07 00:01:53 字数 359 浏览 0 评论 0原文

我正在 apache 上用 php 创建一个非常小的网站,5 个页面。我在顶级目录中创建了五个 php 文件,index.php、random.php 等。我想隐藏 .php 扩展名,因此我将以下内容放入我的 .htaccess 中:

RewriteRule ^random/? $ random.php

如果我访问www.example.com/random,我会得到我需要的页面,但是如果我访问www.example.com/random/(末尾有斜杠),CSS和链接都是下一个目录,即服务器认为我在/random/index.php,而不是/random.php。

我对 RewriteRule 完全是菜鸟,所以提前致谢!

I am creating a very small site, 5 pages, in php on apache. I've created the five php files in the top directory, index.php, random.php, etc. I wanted to hide the .php extension, so I put the following into my .htaccess:

RewriteRule ^random/?$ random.php

If I visit www.example.com/random I get the page I needed, but if I visit www.example.com/random/ (slash at the end), the css and links are all one directory down, i.e. the server thinks I am in /random/index.php, not at /random.php.

I'm a total noob at RewriteRule, so thanks in advance!

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

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

发布评论

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

评论(1

野侃 2024-10-14 00:01:53

对于实际执行 HTTP 重定向的尾部斜杠,您应该有一个单独的规则。例如:

RewriteRule ^random/$ /random [R=301,L]
RewriteRule ^random$ random.php

相对 URL 由浏览器处理,因此带有尾部斜杠的浏览器会感到困惑。

大多数 HTTP 服务器实际上对目录名执行与此类重定向相反的操作。也就是说,如果您访问 http://example.com/foo/bar 并且 bar 是一个目录,您将被重定向到 /foo/栏/

You should have a separate rule for the trailing slash that actually does an HTTP redirect. eg:

RewriteRule ^random/$ /random [R=301,L]
RewriteRule ^random$ random.php

Relative URLs are handled by the browser, so with that trailing slash the browser will get confused.

Most HTTP servers actually do the inverse of this sort of redirect for directory names. That is, if you go to http://example.com/foo/bar and bar is a directory, you'll be redirected to /foo/bar/.

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