使用index.php的利弊?当路由url时,q=path/而不是index.php/path/


Pros/cons of using index.php?q=path/ instead of index.php/path/ when routing URLs?

我正在编写一个将路由映射到文件的简单方法,我遇到了两种方法。

第一个,我猜大多数框架使用的,是使用$_SERVER['REQUEST_URI']变量来提取index.php之后的所有内容:

RewriteRule ^(.*)$ index.php [QSA,L]
第二种方式在Drupal中使用,路由只是作为查询字符串传递。
RewriteRule ^(.*)$ index.php?q=$1 [QSA,L]

现在,"Drupal方式"对我来说似乎简单多了。使用另一种方法,您必须在$_SERVER['REQUEST_URI']和$_SERVER['SCRIPT_NAME']上使用"爆炸",然后使用类似array_diff_assoc的东西来删除脚本名称和子目录名称,如果有的话。这不是很多工作,但是如果用Drupal的方式你可以简单地提取$_GET['q']值,为什么没有人这样做呢?如果有的话,缺点是什么?

谢谢。

使用q参数的缺点是,如果不重写URL, URL将看起来像…

http://domain.com/?q=something

…相对于清洁(IMO)…

http://domain.com/index.php/something

重写url没有太大的优势或劣势。但是,我将指出,包括最后一个斜杠和斜杠之后的所有内容都存储在_SERVER[PATH_INFO]中,因此不需要解析请求URI。

使用较短的URL技术的主要原因是为了更干净的技术和更好的SEO。搜索引擎认为这两个URL是"相同的":

http://www.domain.com/?b=something

http://www.domain.com/?b=hello

我没有一个很好的解释,所以这里有一些链接,其中有一些非常好的信息:

  • http://blog.hubspot.com/blog/tabid/6307/bid/2261/My-Wife-says-Short-URLs-Yield-Better-Click-Through-Rates-in-SEO-and-she-s-RIGHT.aspx
  • 短URL或长URL的SEO
  • http://googlewebmastercentral.blogspot.com/2008/09/dynamic-urls-vs-static-urls.html

现在有些人以不同的方式实现较短的URL,但这是我发现它们最适合我的方式:

在. htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]

在index.php(或其他php文件)

if(isset($_GET['route']) && $_GET['route'] != NULL && strlen($_GET['route']) > 0)
{
    $split = explode('/', $_GET['route']);
    for($i=1; $i <= count($split)-1; $i++)
    {
        $_GET[$i] = $split[$i];
    }
}

这允许你使用$_GET['1'](或$_GET[1])和所有后续的数字。

URL是这样的:

http://www.domain.com/?b=something

http://www.domain.com/something

http://www.domain.com/?b=something& = hello& c =等等

http://www.domain.com/something/hello/blah

参数可以通过:

$_GET[1] = "something";
$_GET[2] = "hello";
$_GET[3] = "blah";

希望有帮助!