.htaccess 在传递给模板之前将 PATH_INFO 变量重写为空;不从 url 中删除 index.php [英] .htaccess rewriting PATH_INFO vars to empty before passing to template; not removing index.php from url

查看:28
本文介绍了.htaccess 在传递给模板之前将 PATH_INFO 变量重写为空;不从 url 中删除 index.php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家早上好,我希望这会很容易!(我还没喝咖啡呢,怎么起这么早?!)

Morning everyone, I'm hoping this will be an easy one! (I haven't even had coffee yet why am I up so early?!)

无论如何..为你准备了一个二人组:第一(基本)问题,我试图从我的链接中删除 index.php(这似乎几乎不可能,感谢 hostgator :)).经过一连串的搜索,我已经到了/page 重定向到/index.php/page 的地步.现在这总比没有好,但当然仍然不理想.我不得不使用 [R] 来做到这一点.我的 .htaccess 是:

Anyway.. got a two-parter for you: First (basic) issue that I'm trying to remove index.php from my links (which seems to be next to impossible thanks hostgator :) ). After a bunch of searching I've gotten to the point where /page redirects to /index.php/page. Now this is better than nothing, but of course still not ideal. And I had to use [R] to do it. My .htaccess is:

RewriteCond   %{REQUEST_FILENAME}   !-f 
RewriteCond   %{REQUEST_FILENAME}   !-d 
RewriteRule   ^(.*)$                index.php/$1  [R,L]

<小时>

第二个(困难)问题 我需要找出从 url 中删除 index.php 的方法,但仍要确保显示正确的页面控制器(即 url 中的页面).我正在使用 PATH_INFO 来获取 var 并显示正确的页面,并且在 index.php 存在时它可以工作.


Second (hard) issue I need to find away to remove index.php from the url but still make sure the proper page controller (ie what page is in the url) is displayed. I'm using PATH_INFO to grab the var and display the proper page, and it works when the index.php is there.

但是由于某种原因,当您从最后一个 RewriteRule 中删除 R 时,/page 总是默认为主 index.php 模板.这就像 .htaccess 或任何在我的站点可以处理页面之前将 PATH_INFO 重写为空的东西.我想我需要在 PATH_INFO 之前锁定 .htaccess 重写它.

But for some reason when you take the R out of that last RewriteRule, /page just always defaults to the main index.php template. It's like the .htaccess or whatever is rewriting PATH_INFO to empty before my site can process the page. I think I need to way to lock in PATH_INFO before the .htaccess rewrites it.

对于某些测试,我停留在 var_dump(path_segments()); 并且在/index.php/page 上时它给出: array(2) { [0]=>字符串(0) "" [1]=>string(10) "proper-page" }.但是,当您从上述重写规则中取出 R 并访问而不仅仅是/page 时,它​​只会给出:array(2) { [0]=>字符串(0) "" [1]=>string(0) "" }.

For some testing I stuck in var_dump(path_segments()); and when on /index.php/page it gives: array(2) { [0]=> string(0) "" [1]=> string(10) "proper-page" }. However, when you take the R out of the above rewrite rule and access without just /page, it just gives: array(2) { [0]=> string(0) "" [1]=> string(0) "" }.

现在我什至不知道我是否需要担心第二个问题,如果有一个简单的方法来做第一个问题.但至少这就是我现在想要弄清楚的一切.感谢我能得到的所有帮助!

Now I don't even know if I need to worry about the second issue if there's an easy way to do the first. But at least that's everything I'm trying to figure out now. Appreciate all the help I can get!

推荐答案

那么你有两个选择.您可以通过调整重写器为您提供路径作为 GET 参数来跳过使用 PATH_INFO:

You have two options then. Either you skip using PATH_INFO by adapting the rewriterule to give you the path as GET parameter:

  RewriteRule   ^(.*)$     index.php?path=$1  [L,QSA]

因此现在使用 $_GET["path"].

或者从使用 $_SERVER["PATH_INFO"] 切换并从 $_SERVER["REQUEST_URI"] 获取原始传入路径.有时也有替代环境变量,如REDIRECT_PATH_INFO,检查print_r($_SERVER);.

Or switch from using $_SERVER["PATH_INFO"] and get the origianl incoming path from $_SERVER["REQUEST_URI"]. Sometimes there are also alternative environment vars, like REDIRECT_PATH_INFO, check print_r($_SERVER);.

这篇关于.htaccess 在传递给模板之前将 PATH_INFO 变量重写为空;不从 url 中删除 index.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆