Hide Page Extensions (Like StackOverflow)

落花浮王杯 提交于 2019-11-26 22:10:41

问题


I want to hide page extensions like stackoverflow does. How does the following work?

http://stackoverflow.com/tags/foo
http://stackoverflow.com/tags/bar

I've seen a lot of sites that do this, but I still don't know how this is accomplished (I have a LAMP stack).


回答1:


When a web server gets a request for a URL, it has to decide how to handle it. The classic method was to map the head of the URL to a directory in the file system, then let the rest of the URL navigate to a file in the filesystem. As a result, URLs had file extensions.

But there's no need to do it that way, and most new web frameworks don't. They let the programmer define how to map a URL to code to run, so there's no need for file extensions, because there is no single file providing the response.

In your example, there isn't a "tags" directory containing files "foo" and "bar". The "tags" URL is mapped to code that uses the rest of the URL ("foo" or "bar") as a parameter in a query against the database of tag data.




回答2:


What you want is clean URLS and you can do it with apache and .htaccess . There may be a better way, but here's how I have been doing it:

http://evolt.org/Making_clean_URLs_with_Apache_and_PHP




回答3:


That's the beauty and the work of ASP.NET MVC.

No "hiding" - it's just the way ASP.NET MVC handles URL's and maps those "routes" to controller actions on your controller classes.

Quite a big step away from the "classic" ASP.NET Webforms way of doing things.




回答4:


There are a couple of ways to do it under Apache+PHP, but the essential principle is to make a set of URIs (perhaps all URIs, depending on your site, but you may want different scripts to handle different portions of the site) translate to a single PHP file, which is told what object the user has requested.

The conceptually simplest way is to rewrite every URL to a script, which gets the URI through $_SERVER['REQUEST_URI'] and interprets it as it likes.

The URI rewriting can be done with various methods including mod_rewrite, mod_alias and ErrorDocument (see Apache docs).

Another way is to set up more complex URL rewriting (probably using mod_rewrite) to add the path as a GET variable.

There is also the $_SERVER['PATH_INFO'] variable which is loaded with the non-existent portion of the path. This option requires little or no modification to Apache config files, but reduces the flexibility of your URLs a little.




回答5:


Modern web development frameworks have support for elegant urls. Check out Django or Ruby on Rails.




回答6:


If you're using Apache and you simply want to hide the file extensions of static HTML files you can use this .htaccess code:

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f       # if the requested URL is not a file that exists
RewriteCond %{REQUEST_FILENAME} !-d       # and it isn't a directory that exists either
RewriteCond %{REQUEST_FILENAME}\.html -f  # but when you put ".html" on the end it is a file that exists
RewriteRule ^(.+)$ $1\.html [QSA]         # then serve that file

</IfModule>

Apache mod_rewrite has been called "voodoo, but seriously cool voodoo".

The actual .htaccess code I use on a few sites is like that, but not identical:

<IfModule mod_rewrite.c>
    RewriteEngine on

    #RewriteRule ^$ index.php [QSA]
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.+)$ $1\.php [QSA]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)$ index.php/$1 [QSA]

</IfModule>

And here is some much longer but far more readable code to do the same thing on a Zeus server. On Zeus, it's called rewrite.script.

# http://drupal.org/node/46508


# get the document root
map path into SCRATCH:DOCROOT from /
# initialize our variables
set SCRATCH:ORIG_URL = %{URL}
set SCRATCH:REQUEST_URI = %{URL}

match URL into $ with ^(.*)\?(.*)$
if matched then
  set SCRATCH:REQUEST_URI = $1
  set SCRATCH:QUERY_STRING = $2
endif

# prepare to search for file, rewrite if its not found
set SCRATCH:REQUEST_FILENAME = %{SCRATCH:DOCROOT}
set SCRATCH:REQUEST_FILENAME . %{SCRATCH:REQUEST_URI}

# check to see if the file requested is an actual file or
# a directory with possibly an index.  don't rewrite if so
look for file at %{SCRATCH:REQUEST_FILENAME}
if not exists then
  look for dir at %{SCRATCH:REQUEST_FILENAME}
  if not exists then
    look for file at  %{SCRATCH:REQUEST_FILENAME}.php
    if exists then
        set URL = %{SCRATCH:REQUEST_URI}.php?%{SCRATCH:QUERY_STRING}
    else
        set URL = /index.php/%{SCRATCH:REQUEST_URI}?%{SCRATCH:QUERY_STRING}
    endif
  endif
endif
goto END


来源:https://stackoverflow.com/questions/1759420/hide-page-extensions-like-stackoverflow

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