开发者

Why some URL's of websites doesn't show the .php extention just shows words like stackoverflow.com/questions/ask

Hello guys I was wondering why some websites which uses php (or any other server side scripting language) doesn't show the extention and even the file. like this url stackoverflow.com/questions/ask and also some websites gets data from users, let's say a website that lets users post some random post, ok so when we click on that post it sends us to : domain.com/post/what-user-posted-goes-here and if we try to go to domain.com/post/ we get an error of page not found.

I'm asking this question because because I want to know what is the method开发者_Python百科 used and is it related to php ? and because I want to use it.


It's because they use Apache's mod_rewrite:

http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

You can easily reroute URLs with it, so if you want you can make /test point to /pages/test.html for instance.

RewriteEngine on
RewriteRule   test  /pages/test.html

Usually, people use regex to make dynamic rerouting such as:

RewriteEngine on
RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]

So /page/test-page will show index.php?page=test-page


What you are talking about is the search engine optimization (SEO) of URLs.

See How to Make your URLs SEO Friendly and SEO friendly URL in PHP for more details as this subject is very well documented already here on SO and on the Web in general.


if you put domain.com/post/ it will look for an index.php or index.html for example. If it cant find one of those, it will error out. Its not really related to PHP at all :)


Just in case, Stack Overflow is built on ASP.NET.

The key word you're looking for is 'extensionless URLs.' Implementation depends upon what server you're using.

For Apache and PHP, see this answer: .htaccess: non-www to www + extensionless urls + no index


Many sites use a kind of gateway script that route all requests. Usually in those cases something like mod_rewrite is used to direct requests to that script.

For example: http://example.com/blog/my-first-post and http://example.com/blog/my-second-post may both be rewritten to /index.php. That index script could then parse the request to determine that a blog post is being requested, as well as which specific post.


The web server controls what requests get sent to which programs. The simplest method is what you described, when domain.com/folder/file.ext gets sent to the handler for the file. This may be PHP in the case of a .php file, or the file may just get displayed as is, in the case of an HTML file.

Another possibility is what benhowdle89 described, which is also the default behavior for most web servers - if you ask for domain.com/folder/, it checks for index.html, or index.php, or things like that.

In the case of sites like SO, the mapping is different. The webserver is configured to take a request like /questions/4339829/why-some-urls-of-websites-doesnt-show-the-php-extention-just-shows-words-like and send that request to a program that knows what to do with it. For example, it could be something like /scripts/displayquestion.php?question_id=4339828, or it could be any other program that is written to handle the requests. This could be a PHP script, or it could be an application server, or some script written in another language.


The way to achieve this is simple. When the user is pointed to a directory, the server looks for the root of that directory (index.php).

So in your example, domain.com/post/ you would have a directory (folder) called post. Inside that folder you would have index.php. The server would return the index.php file in that folder and the URL would not display the php file, just the URL domain.com/post/.

If the server were to display the php file, it would look something like: domain.com/post/index.php, however the index.php doesn't display.

The purpose, like the others who have commented, is for SEO (Search Engine Optimization) as well as a safeguard in the event that you change what you are developing your site on, you won't lose your SEO links or create dead links when people bookmark your site.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜