帮助解决简单的 RewriteRule 问题
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于实际执行 HTTP 重定向的尾部斜杠,您应该有一个单独的规则。例如:
相对 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:
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
andbar
is a directory, you'll be redirected to/foo/bar/
.