隐藏页面扩展(如 StackOverflow)

发布于 2024-08-11 09:53:29 字数 187 浏览 6 评论 0原文

我想像 stackoverflow 那样隐藏页面扩展。以下是如何工作的?

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

我见过很多网站都这样做,但我仍然不知道这是如何完成的(我有一个 LAMP 堆栈)。

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

摘星┃星的人 2024-08-18 09:53:30

在 Apache+PHP 下有几种方法可以做到这一点,但基本原则是对一组 URI(也许是所有 URI,具体取决于您的站点,但您可能需要不同的脚本来处理站点的不​​同部分)进行翻译到单个 PHP 文件,该文件被告知用户请求了哪个对象。

概念上最简单的方法是将每个 URL 重写为脚本,该脚本通过 获取 URI $_SERVER['REQUEST_URI'] 并按需要解释它。

URI 重写可以使用多种方法完成,包括 mod_rewrite、mod_alias 和 ErrorDocument(请参阅 Apache 文档)。

另一种方法是设置更复杂的 URL 重写(可能使用 mod_rewrite)以将路径添加为 GET 变量。

还有 $_SERVER['PATH_INFO'] 变量,该变量加载了路径中不存在的部分。此选项几乎不需要修改 Apache 配置文件,但会稍微降低 URL 的灵活性。

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.

暮色兮凉城 2024-08-18 09:53:30

现代 Web 开发框架支持优雅的 url。查看 DjangoRuby on Rails

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

酒几许 2024-08-18 09:53:30

如果您使用 Apache 并且您只是想隐藏静态 HTML 文件的文件扩展名,您可以使用此 .htaccess 代码:

<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 已被称为“voodoo,但是非常酷的巫毒”。

我在一些网站上使用的实际 .htaccess 代码与此类似,但并不完全相同:

<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>

这里有一些更长但更具可读性的代码,可以在 Zeus 服务器上执行相同的操作。在 Zeus 上,它被称为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

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
初与友歌 2024-08-18 09:53:29

当 Web 服务器收到 URL 请求时,它必须决定如何处理它。经典方法是将 URL 的头部映射到文件系统中的目录,然后让 URL 的其余部分导航到文件系统中的文件。因此,URL 具有文件扩展名。

但没有必要这样做,而且大多数新的 Web 框架也不需要这样做。它们让程序员定义如何将 URL 映射到要运行的代码,因此不需要文件扩展名,因为没有单个文件提供响应。

在您的示例中,没有包含文件“foo”和“bar”的“tags”目录。 “tags”URL 映射到使用 URL 的其余部分(“foo”或“bar”)作为针对标签数据数据库的查询中的参数的代码。

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.

不羁少年 2024-08-18 09:53:29

您想要的是干净的 URL,您可以使用 apache 和 .htaccess 来做到这一点。可能有更好的方法,但我一直在这样做:

http://evolt.org/Making_clean_URLs_with_Apache_and_PHP

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

黑白记忆 2024-08-18 09:53:29

这就是 ASP.NET MVC 的魅力和工作。

没有“隐藏”——这只是 ASP.NET MVC 处理 URL 并将这些“路由”映射到控制器类上的控制器操作的方式。

与“经典”ASP.NET Webforms 的做事方式相去甚远。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文