PATH_INFO in PHP without having file apparent

。_饼干妹妹 提交于 2019-12-11 16:49:36

问题


I have a simple script:

index.php:

<?php

$path= $_SERVER['PATH_INFO'];

if($path)
  echo $path;
else
  echo "No Path Info";

?>

When I run it like so www.website.com/index.php it works. ie) www.website.com/index.php/hello will echo /hello

However, if I go to www.website.com/hello, I get a URL not found error when what I want is that /hello is echoed.

How do I make it so that index.php doesn't have to be present for PATH_INFO to work?!


回答1:


When a webpage is requested from a server. The server looks at the path (i.e. example.com/this/is/a/path) to figure out which file to serve and from where.

As I understand it, what you want to do is have your index.php handle all the requests. The way you would do this to use URL Rewriting.

Assuming you are using an apache web server, you can use something called *mod_rewrite* to do this. See more on mod_rewrite here.

For the specific rules to use, you probably want to use something like the code in V_K's answer.




回答2:


If you are using apache web server - write this rule in your .htaccess..

RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /index.php/$1 [L]



回答3:


you will have to rewrite in the url settings depending on what server you are using to use clean url's

check this http://wettone.com/code/clean-urls

something on these lines

RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /index.php/$1 [L]


来源:https://stackoverflow.com/questions/15168466/path-info-in-php-without-having-file-apparent

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