using seo user friendly in php

2019-01-12 08:11发布

问题:

this is the URL path I currently use:

/index.php?page=1&title=articles

I want to get the URL path as

/index/page-1/title-articles

using SEO user friendly URLs in PHP.

And how to get the value of the "title"? Any one can help me pls.

回答1:

Check out the mod_rewrite module, and maybe for a good starting point, this tutorial on how to take advantage of it with PHP.



回答2:

You need to ensure two things:

  1. your application prints out the new URLs properly, and
  2. your webserver can understand that new URLs and rewrites them to your internal scheme or redirects them back to your application and your application does the rest.

The first part can be simply accomplished by using

echo '<a href="/index/page-1/title-articles"> … </a>';

instead of

echo '<a href="/index.php?page=1&title=articles"> … </a>';

The second part can be accomplished either with URl mapping features of your webserver (most webservers have a module like Apache’s mod_rewrite). With mod_rewrite, the following will do the rewrite:

RewriteEngine on
RewriteRule ^index/([^/-]+)-([^/]+)(.*) /index$3?$1=$2 [N,QSA]
RewriteRule ^index$ index.php [L]

The first rule will extract one parameter at a time and append it to the query. The second rule will finally rewrite the remaining /index URL path to /index.php.



回答3:

I want to get the URL path as

/index/page-1/title-articles

Why? I’ve got two objections:

  1. index is a no-information and I doubt that it belongs in the URI
  2. page-1, as well as title-articles looks plain weird. Like with index, you should ask yourself whether this information belongs here. If it does, make clear that it’s the key of a key-value pair. Or remove it entirely.

Thus, I propose either:

/‹article›/1

or

/‹article›/page/1

or

/‹article›/page=1

or

/‹article›[1]

or

/articles/‹article›/page/1

Or any combination thereof. (In the above, ‹article› is a placeholder for the real title, the other words are literals).