How to rewrite SEO friendly url like stackoverflow

If you go to the URL, for example: 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, but not with a hard update, I think it is done with mod_rewrite; however, how does he know the title of the article without server-side processing using php?

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

 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 way of making sure that Google doesn't hit me on duplicate content and that bookmarks are being sent correctly:

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

This solution is very short-sighted, and thousands of lines fit into my .htaccess.

+4
source share
5 answers

how does he know the name of the article without the server side processing using php?

mod_rewrite can only do a lot. In addition, you will have to upgrade it too much. It is best to handle these requests with scripts. Create .htaccess , similar to what you did:

 <IfModule mod_rewrite.c> RewriteEngine On #RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ /index.php [QSA,L] </IfModule> 

Then, in index.php split REQUEST_URI into parsing /review/1/ , look into the database for the review title using id of 1, pass it to your friendly URI function (slug), and add it to the url, then redirect .

+3
source

for duplicate content, you must have a canonical link tag in your head. the canonical link tag tells google that the current page has an alternate URL for the same content as the canonical link, and the canonical link is the main URL for this content. this is great for things like paginated content, search, etc., where the same page can have different URLs.

 <link rel="canonical" href="http://www.somedomain.com/mainurl" /> 
+3
source

I also created a SEO-friendly URL solution for our show. The url is naturally /raido/episode.php?SeriesId=1&EpisodeId=123, but I also 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 allow the destination page to process it.

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

It also allows you to continue to accept existing links without having two versions of the same code.

+2
source

if i do

 curl -I http://stackoverflow.com/questions/9155602/ 

I really get a hard redirect:

 HTTP/1.1 301 Moved Permanently Cache-Control: public, max-age=60 Content-Length: 0 Content-Type: text/html Expires: Mon, 06 Feb 2012 22:43:27 GMT Last-Modified: Mon, 06 Feb 2012 22:42:27 GMT Location: /questions/9155602/redirect-existing-file-to-different-url-using-mod-rewrite Vary: * Date: Mon, 06 Feb 2012 22:42:27 GMT 

often these settings contain a short .htaccess, causing all requests to go through the front controller or the main index.php file. this file checks the $ _SERVER ['PATH_INFO'] variable and decides which content to draw in and display.

this is a simple .htaccess example (actually just copied by installing vanilla Wordpress):

 RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] 

in this example, with hard redirects, they can have an โ€œelseโ€ in index.php, doing a search and redirecting correctly if the content is not found based on the last PATH_INFO fragment.

+2
source

You need to either do this in the application itself by checking the URL against the absolute URL for the requested location, or by generating a display each time a new "page" is added.

0
source

Source: https://habr.com/ru/post/1395110/


All Articles