.htaccess rewriting PATH_INFO vars to empty before passing to template; not removing index.php from url

旧巷老猫 提交于 2021-02-10 17:50:44

问题


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

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]

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.

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.

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!


回答1:


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]

Thus use $_GET["path"] now.

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);.



来源:https://stackoverflow.com/questions/15042344/htaccess-rewriting-path-info-vars-to-empty-before-passing-to-template-not-remo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!