如何通过URL接受GET参数,但没有“.php?参数=值”


How to accept GET arguments through a URL, but without having ".php?argument=value"

可能的重复项:
如何:在 PHP 中重写 URL?

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

例如,您可以访问:

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

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

我可以用:

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

$url = $_GET['url'];

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

多谢!PIMVDB指出,这是这个问题的重复:

如何在PHP中进行URL重写?

你需要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]

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

每次我这样做时,我都使用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);

你可以用mod_rewrite

.htaccess 文件内容示例:

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