Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
5.7k views
in Technique[技术] by (71.8m points)

php - How to rewrite SEO friendly url's like stackoverflow

If you go to a url like the following: http://stackoverflow.com/questions/9155602/ the address bar will be updated to http://stackoverflow.com/questions/9155602/redirect-existing-file-to-different-url-using-mod-rewrite. This is not done with JavaScript and not with a hard-refresh, my best guess is that it's done with mod_rewrite; however, how would it know the title of the article without server side processing with php?

I just updated my site to use SEO friendly url's, it used to be /shows.php?id=review-1 and now it's /review/1/super-baseball-2020. The text at the end doesn't actually really matter when it comes to my rewrite which is:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9]+)/([0-9]+)/([a-z0-9-]+)?/?$ /shows.php?id=$1-$2 [L,NC]

My current path of making sure Google doesn't hit me for duplicate content, and that bookmarks get forwarded properly is the following:

RewriteCond %{THE_REQUEST} ^GETs/shows.php?
RewriteCond %{QUERY_STRING} (?:^|&)id=review-1(?:&|$) [NC]
RewriteRule ^ /review/1/super-baseball-2020/? [R=301,L,NC]
RewriteCond %{THE_REQUEST} ^GETs/shows.php?
RewriteCond %{QUERY_STRING} (?:^|&)id=review-2(?:&|$) [NC]
RewriteRule ^ /review/2/aero-fighters-2/? [R=301,L,NC]
RewriteCond %{THE_REQUEST} ^GETs/shows.php?
RewriteCond %{QUERY_STRING} (?:^|&)id=review-3(?:&|$) [NC]
RewriteRule ^ /review/3/aero-fighters-3/? [R=301,L,NC]
.... and so on ....

That solution is very shortsighted and is thousands of lines to put into my .htaccess.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I also have created an SEO-friendly URL solution for our show. The URL is naturally /raido/episode.php?SeriesId=1&EpisodeId=123, but I too wanted /radio/1-123-live.htm, so my .htaccess looks like this:

RewriteRule ^d{1,3}-d{1,4}(.+).html$ episode.php?Slug=$0

I then let the destination page handle it

if ($Slug !== "") {
  $lIds = explode("-", $Slug);
  $SeriesId=$lId[0];
  $EpisodeId=$lIds[1];
}

This also allows you to continue to accept existing links without having to have 2 versions of the same code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...