問題描述
如果您訪問如下網址:http://stackoverflow.com/questions/9155602/
地址欄將更新為 http://stackoverflow.com/問題/9155602/redirect-existing-file-to-different-url-using-mod-rewrite
.這不是用 JavaScript 完成的,也不是用硬刷新完成的,我最好的猜測是它是用 mod_rewrite 完成的;但是,如果沒有服務器端用php處理,它怎么知道文章的標題?
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?
我剛剛更新了我的網站以使用 SEO 友好的 url,以前是 /shows.php?id=review-1
,現在是 /review/1/super-baseball-2020
.當涉及到我的重寫時,最后的文本實際上并不重要,即:
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]
我目前確保 Google 不會因為重復內容而向我發送信息,并且書簽得到正確轉發的方法如下:
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 ....
該解決方案非常短視,需要在我的 .htaccess 中放入數千行.
That solution is very shortsighted and is thousands of lines to put into my .htaccess.
推薦答案
沒有服務器端怎么知道文章標題用php處理?
how would it know the title of the article without server side processing with php?
mod_rewrite
只能做這么多.另外,你必須更新太多.最好使用腳本處理這些請求.創建一個 .htaccess
,類似于你所做的:
mod_rewrite
can only do so much. Plus, you'd have to update it too much. It's best to handle these requests with scripting. Create an .htaccess
, similar to what you've done:
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php [QSA,L]
</IfModule>
然后在您的 index.php
中,拆分 REQUEST_URI
以解析出 /review/1/
,在數據庫中查找id
為 1 的評論標題,將其傳遞給您的友好 URI (slug) 函數,并將其附加到 url,然后重定向.
Then in your index.php
, split the REQUEST_URI
to parse out the /review/1/
, look in the database for the title of the review with id
of 1, pass it to your friendly URI (slug) function, and append it to the url, then redirect.
這篇關于如何像stackoverflow一樣重寫SEO友好的url的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!