Вопрос

I am doing on a project and i stumble upon on this while going through some code. I had tried to google but could not find a correct keyword to search regarding this question.

http://www.fruit.com/example.php/red

I am confuse with this and the end of a .php it still have a character /red for example. What is the purpose of this? Does it have something to do with POST method?

Это было полезно?

Решение

A possible explanation is that the developer is using .htaccess rewrite rules. It is most likely extra data that has to do with the page (such as you mentioned, POST and GET) and it's only done this way to make the URL look prettier or to simplify the URL.

It is also sometimes done this way to make it easier to linkback to this page, especially if the page is being generated based on the data.

A popular place to do this would be blogs. You often see blogs display links as:

http://blog.com/blog/2001/02/23/how-pretty-is-this

when in reality, the request to the server is something like:

/blog.php?year=2001&month=02&day=23&title=how-pretty-is-this

which isn't that pretty and not as easy to link-back to this particular page.

Другие советы

Mr.Xenotype answered your question well but missed one little part.

Does it have something to do with POST method?

No, it deals with the GET method. As he stated, it's pretty much to make the URL look "pretty", and it could actually be a crucial part of a RESTful API (bit advanced for a beginner) if that's how the site was designed.

http://www.fruit.com/example.php/red

Is the same as

http://www.fruit.com/example.php?id=red

Some tinkering with the .htaccess file is required to accomplish this using what are called regular expressions, or REGEX. If you'd like to know more I'll gladly provide some examples.

Adding a "RewriteRule" to the .htaccess file (a server configuration file) can allow the server to change the way the server files/folders are accessed

//probably what the rewrite looks like of the example you provided
RewriteRule .* example.php/$0 [L]

RewriteRules are broken into 4 blocks: action, pattern, rewrite and flag

Action: RewriteRule, essentially just lets the server know we're going to be performing a rewrite

Pattern: .* is our pattern in this example. The "." stands for any character, meaning match any character, and the "*" tells it to repeat this. So, match any character after the rewrite.

The rewrite is the page to perform this on, so example.php is the rewrite. The "$" of this tells it where to start the rewrite. So, we'll start the rewrite at the "$" symbol, and use our pattern. Our pattern ".*" tells us to match any character, in this case, red.

The [L] means last rule. This is it, no more rules to follow. You can use multiple rewrite rules for multiple pages.

So now, instead of using example.php?id=red, the server now knows that starting after "example.php/" to match any characters we supply.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top