【问题标题】:How to process URL without specifying parameters如何在不指定参数的情况下处理 URL
【发布时间】:2010-11-28 06:32:00
【问题描述】:
我想知道是否可以在不指定参数的情况下处理 URL。例如:
http://www.example.com/Sometext_I_want_to_process
我不想使用:http://www.example.com/index.php?text=Sometext_I_want_to_process
网站在处理后必须重定向到不同的页面。
我有什么语言选择?
【问题讨论】:
标签:
php
url
process
parameters
【解决方案1】:
我建议使用 apache 的 mod_rewrite(在其他网络服务器上可以找到类似的功能)来重写 URL,使其成为一个参数。比如你使用的 text 参数,你可以使用下面的 mod_rewrite 规则来获取参数。
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico # Want favicon.ico to work properly
RewriteRule ^(.*)$ index.php?text=$1 [L,QSA]
然后您只需像往常一样访问脚本中的参数。
<?php
$stuff = $_GET['text'];
// Process $stuff
【解决方案2】:
您可以使用 Apache 的 mod_rewrite 来做这种事情。
显然,这意味着它必须启用——默认情况下通常不是这样。
例如,在一个网站上,我在 .htaccess 文件中使用它:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.*)$ /index.php?hash=$1 [L]
这会将所有内容重定向,例如 www.mysite.com/152 到 www.mysite.com/index.php?hash=152
然后,在我的 PHP 代码中,我可以使用 $_GET :
if (isset($_GET['hash'])) {
if (is_numeric($_GET['hash'])) {
// Use intval($_GET['hash']) -- I except an integer, in this application
}
}
在您的情况下,您可能希望将“hash”替换为“text”,但这应该已经帮助您更接近解决方案;-)