【问题标题】:How to accept GET arguments through a URL, but without having ".php?argument=value" [duplicate]如何通过 URL 接受 GET 参数,但没有“.php?argument=value”[重复]
【发布时间】:2012-01-30 15:23:54
【问题描述】:

可能重复:
How to: URL re-writing in PHP?

网站如何使用您直接在 URL 中提供的参数?

例如,您可以访问:

http://isup.me/www.google.com

然后该站点上的脚本可以使用值 www.google.com 来查看该站点是否已启动。

我可以这样做:

http://isup.me/index.php?url=ww.google.com

$url = $_GET['url'];

但这不是很干净,我很想知道它是如何以另一种方式完成的。

非常感谢! pimvdb 指出这是这个问题的重复:

How to do URL re-writing in PHP?

【问题讨论】:

  • 一些框架提供了更直接的方法,但实际上你所拥有的几乎就是答案。您可能会想出一个自己的迷你框架来根据某种一致的模式或其他方式拆分 URL。
  • 在 apache 中使用 mod_rewrute,或者在您使用的任何 httpd 中等效。数据仍在 $_GET 中,但其格式被 URL 重写隐藏。这就是通常的做法。
  • 如何让表单将用户发送到修改后的 URL?它是否总是涉及 Javascript 或重定向?

标签: php javascript url


【解决方案1】:

你需要ModRewrite

# Turn on URL rewriting
RewriteEngine On
RewriteBase /

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [L]

【讨论】:

    【解决方案2】:

    这完全取决于您的服务器配置。

    每次我这样做时,我都将mod_rewrite 用于 apache:(.htaccess 文件)

    RewriteEngine On
    RewriteBase /
    #If not an existing File
    RewriteCond %{REQUEST_FILENAME} !-f
    #AND If not an existing Directory
    RewriteCond %{REQUEST_FILENAME} !-d
    #rewrite to test.php
    RewriteRule ^(.*) /test.php?site=$1 [L]
    

    您可以使用 php 文件对此进行测试:

    <?php
    print_r($_REQUEST);
    

    【讨论】:

      【解决方案3】:

      您可以为此使用 mod_rewrite!

      .htaccess 文件内容示例:

      RewriteEngine On
      RewriteCond %{REQUEST_URI}
      RewriteCond %{REQUEST_FILENAME} !index.php
      RewriteRule ^([^/]*)$ index.php?get_var_name=$1
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-25
        • 2017-08-07
        • 1970-01-01
        • 1970-01-01
        • 2015-04-18
        • 2012-10-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多