简体   繁体   English

漂亮的URL和SEO友好的URL?

[英]Pretty URL & SEO-friendly URL?

I'm currently using Apache 2.2 我目前正在使用Apache 2.2

I can do simple things like 我可以做一些简单的事情

RewriteRule ^/news$ /page/news.php [L]
RewriteRule ^/news/(.*)$ /page/news.php?id=$1 [L]

but what if I want to send 2 parameters like this 但是如果我想发送2个参数怎么办

http://www.example.com/link/param1/param1_value/param2/param2_value http://www.example.com/link/param1/param1_value/param2/param2_value

Lastly, I want to also know implementing SEO friendly URL like stackoverflow 最后,我还想知道实现SEO友好的URL,例如stackoverflow

though I can get access to a page with URL like 虽然我可以访问带有URL的页面,例如

http://www.example.com/doc_no/ http://www.example.com/doc_no/

Just decorating that URL with 只需使用

http://www.example.com/doc_no/this-is-the-article http://www.example.com/doc_no/this-is-the-article

Give me some suggestion with example snippets. 给我一些示例片段的建议。

I know that the PHP symfony framework allows you to do that. 我知道PHP symfony框架允许您执行此操作。

How does it work : In apache config, use mod_rewrite to redirect ALL incoming resquest to a single entry point (in symfony this is called the "front controller") 工作原理:在apache配置中,使用mod_rewrite将所有传入的请求重定向到单个入口点(在symfony中,这称为“前端控制器”)

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

In this front controller you are going to create a "Request" object which holds all the informations provided by the URL. 在此前端控制器中,您将创建一个“请求”对象,其中包含URL提供的所有信息。

For example you could say that the first thing after the "/" is the name of the PHP file to call and everything else are parameters and values so that : http://example.com/file/id/2 will call file.php with id=2 例如,您可以说“ /”之后的第一件事是要调用的PHP文件的名称,而其他所有内容都是参数和值,因此: http : //example.com/file/id/2将调用文件。 PHP的ID = 2

To do that, just use some reg exp and design you "Request" class carefully. 为此,只需使用一些reg exp并仔细设计“请求”类即可。 For the example above the "Request" class should provide both getRequestedAction() and getParameter(string parameter) methods. 对于上面的示例,“ Request”类应同时提供getRequestedAction()和getParameter(string parameter)方法。 The getRequestedAction() method will be used when the "Request" object is fully populated in order to call the correct file/action/method. 当完全填充“请求”对象时,将使用getRequestedAction()方法以调用正确的文件/操作/方法。

if you choose to populate the parameter array of the request object with both reg exp on the URL and a parsing of the _GET array, you may get to the point where : http://example.com/file/id/2 is the same as http://example.com/file?id=2 (and both can work) 如果您选择使用URL上的reg exp和_GET数组的解析填充请求对象的参数数组,则可能会到达以下位置: http : //example.com/file/id/2是与http://example.com/file?id=2相同(两者都可以使用)

you can choose to ignore extensions (http://example.com/file.html is the same as http://example.com/file ), or not. 您可以选择是否忽略扩展名(http://example.com/file.html与http://example.com/file相同)。

Finally, for some URL, you can choose to just ignore everything that goes after the last '/'. 最后,对于某些URL,您可以选择忽略最后一个'/'之后的所有内容。 So that : http://example.com/question/3/where-is-khadafi is the same as http://example.com/question/3/is-linux-better-than-windows 因此: http : //example.com/question/3/where-is-khadafihttp://example.com/question/3/is-linux-better-than-windows相同

In the different file.php, just use $request->getParameter('id') to get the value of the "id" parameter, instead of using the _GET or _POST arrays. 在不同的file.php中,只需使用$ request-> getParameter('id')即可获取“ id”参数的值,而不是使用_GET或_POST数组。

The whole point is to 重点是

  1. Redirect all incoming traffic to a single "front controller" 将所有传入流量重定向到单个“前端控制器”
  2. In that file, create a "Request" object that contains all the informations needed to run the site 在该文件中,创建一个“请求”对象,其中包含运行站点所需的所有信息
  3. Call the correct action (php file) based on the informations contained in that "Request" object 根据该“请求”对象中包含的信息,调用正确的操作(php文件)
  4. Inside the actions, use this request object to fetch the parameters contained in the URL 在操作内部,使用此请求对象来获取URL中包含的参数

Hope this helps 希望这可以帮助

Note Google have stated that they prefer news.php?id=$1 instead of news/$1 because it is easier for them to detect the variable. 注意Google声明他们更喜欢news.php?id=$1而不是news/$1因为它们更容易检测到该变量。 This is more pertinent when increasing the number of variables as just looking at your first example is a bit confusing: 当增加变量的数量时,这更相关,因为仅看您的第一个示例就有些混乱:

http://www.example.com/link/param1/param1_value/param2/param2_value

You can always combine the two if one parameter is generic like a category: 如果一个参数是通用的(例如类别),则可以始终将两者组合:

http://www.example.com/param1/?id=param2_value

One should really reevaluate the design if more than one parameter is required and it is not a temporary search. 如果需要多个参数并且不是临时搜索,则应该重新评估设计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM