Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when i put a slash (/) after a .php url?

I have a simple question, but could not find the answer anywhere

say I have a site "mysite.com". I can access the index page by either typing "mysite.com" or "mysite.com/index.php". This works fine... however, when i try to go to "mysite.com/index.php/" the page loads, but not correctly. what is exactly happening? I would think it should return a 404 error, since index.php would be treated as a (non existing) directory (i.e. i would think it would try to find "mysite.com/index.php/index.php"). This clearly isn't the case. Can someone please tell me what exactly is happening? This is also true when you put anything after the slash, i.e. "mysite.com/index.php/hello"

thanks.

like image 222
Guy Avatar asked Jan 23 '14 04:01

Guy


People also ask

Can a URL end with a slash?

A trailing slash is a forward slash placed at the end of a URL. It's usually used to indicate a directory (as opposed to a file), but in SEO it can affect your rankings.

Do you need forward slash at end of URL?

A trailing slash is a forward slash (“/”) placed at the end of a URL such as domain.com/ or domain.com/page/. The trailing slash is generally used to distinguish a directory which has the trailing slash from a file that does not have the trailing slash. However, these are guidelines and not requirements.

What is a slash in URL?

Historically, it's common for URLs with a trailing slash to indicate a directory, and those without a trailing slash to denote a file: http://example.com/foo/ (with trailing slash, conventionally a directory) http://example.com/foo (without trailing slash, conventionally a file)

Should base URL include trailing slash?

Many webmasters want to know whether to include a trailing slash (/) at the end of URLs. This has potential implications for SEO because search engines like Google don't always see different URL structures as equivalent. The short answer is that the trailing slash does not matter for your root domain or subdomain.


2 Answers

This is due to your Apache environmental variable called PATH_INFO.

PATH_INFO

Actually, PATH_INFO is related to the Apache Web Server serving PHP pages and not PHP per-say.

PATH_INFO is an environment variable set by Apache when the AcceptPathInfo directive is turned on. It will contain trailing pathname information that follows an actual filename (or non-existent file in an existing directory) will be accepted or rejected. Environment variables are then passed on to the Apache/CGI module in charge of rendering the page.

The variable is accessible in PHP using $_SERVER['PATH_INFO'].

For example, assume the location /test/ points to a directory that contains only the single file here.html. Then requests for /test/here.html/more and /test/nothere.html/more both collect /more as PATH_INFO.

This answer is copied from Andrew Moore Link to original answer

like image 140
Lal krishnan S L Avatar answered Oct 24 '22 18:10

Lal krishnan S L


When the server notices that a "directory" in the URL is a script rather than an actual directory, it runs the script. The remaining components of the path in the URL are put in the PHP variable $_SERVER['PATH_INFO'].

like image 36
Barmar Avatar answered Oct 24 '22 17:10

Barmar