PATH_INFO in PHP without having file apparent

2019-09-09 17:39发布

问题:

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]


标签: php pathinfo