简体   繁体   中英

Pretty URL & SEO-friendly URL?

I'm currently using 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

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

Lastly, I want to also know implementing SEO friendly URL like stackoverflow

though I can get access to a page with URL like

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

Just decorating that URL with

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.

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")

<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.

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

To do that, just use some reg exp and design you "Request" class carefully. For the example above the "Request" class should provide both getRequestedAction() and getParameter(string parameter) methods. The getRequestedAction() method will be used when the "Request" object is fully populated in order to call the correct file/action/method.

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)

you can choose to ignore extensions (http://example.com/file.html is the same as http://example.com/file ), or not.

Finally, for some URL, you can choose to just ignore everything that goes after the last '/'. So that : http://example.com/question/3/where-is-khadafi is the same as http://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.

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
  4. Inside the actions, use this request object to fetch the parameters contained in the 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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