[英]ReWrite Rule for URLS
我有2000個網址,格式為:-
http://www.example.com/home/2015/02/yet-another-page-title-goes-here.html
http://www.example.com/home/2015/01/another-page-title-goes-here.html
http://www.example.com/home/2014/12/page-title-goes-here.html
這些是舊站點的啟用了永久鏈接的WordPress的OLD URL。
我要剝離這些URL的兩個部分。 我想刪除/ home /,最后想刪除.html。
這意味着如果有人點擊了舊鏈接(在Google或其他地方),他們將進入該內容的新URL。
例如,有人嘗試訪問舊的過期鏈接:
http://www.example.com/home/2015/01/another-page-title-goes-here.html
將重定向到新鏈接:-
http://www.example.com/2015/01/another-page-title-goes-here
在過去的幾天里,我嘗試在WordPress中編輯htaccess文件,但到目前為止沒有任何樂趣。 重定向規則對我來說是新的。
我已經整理過類似的東西,但是沒有用
RewriteRule ^home/(.+)\.html $
如果啟用了永久鏈接,該代碼是否正確添加到WordPress的htaccess文件中的何處。
這是默認情況下我的htaccess文件的內容。 w3cache也在頂部添加了一些內容。
# BEGIN W3TC Browser Cache
<IfModule mod_deflate.c>
<IfModule mod_headers.c>
Header append Vary User-Agent env=!dont-vary
</IfModule>
AddOutputFilterByType DEFLATE text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/html text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon application/json
<IfModule mod_mime.c>
# DEFLATE by extension
AddOutputFilter DEFLATE js css htm html xml
</IfModule>
</IfModule>
# END W3TC Browser Cache
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
任何想法將不勝感激,
建議使用代碼更新的HTACCESS文件:
# BEGIN W3TC Browser Cache
<IfModule mod_deflate.c>
<IfModule mod_headers.c>
Header append Vary User-Agent env=!dont-vary
</IfModule>
AddOutputFilterByType DEFLATE text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/html text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon application/json
<IfModule mod_mime.c>
# DEFLATE by extension
AddOutputFilter DEFLATE js css htm html xml
</IfModule>
</IfModule>
# END W3TC Browser Cache
RewriteEngine On
RewriteRule ^home/(.+)\.html $
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^home/(.+)\.html$ /$1 [R,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteRule
具有多個參數。 您只指定了一個要匹配的URI。 您應該使用以下格式:
RewriteRule ^home/(.+)\.html$ $1 [R,L]
首先,我們匹配home/<everything>.html
並重定向到<everything
,用$1
表示。
如果新規則對您有效,則可以安全地將R
更改為R=301
以使更改永久生效-也就是說,搜索引擎和瀏覽器將在更長的時間內緩存重定向。 沒有它,您基本上是在說重定向是臨時的,並且不應緩存它。
您的.htaccess
文件現在應該如下所示:
# BEGIN W3TC Browser Cache
<IfModule mod_deflate.c>
<IfModule mod_headers.c>
Header append Vary User-Agent env=!dont-vary
</IfModule>
AddOutputFilterByType DEFLATE text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/html text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon application/json
<IfModule mod_mime.c>
# DEFLATE by extension
AddOutputFilter DEFLATE js css htm html xml
</IfModule>
</IfModule>
# END W3TC Browser Cache
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^home/(.+)\.html$ $1 [R,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>
# END Wordpress
竊取@Mike的答案后,我又打斷並修復了:
對於永久更改,請輸入以下行:
RewriteRule ^home/(.+)\.html$ /$1 [R=301,L]
前行:
RewriteRule ^index\.php$ - [L] # this line means never redirect index.php
它看起來應該像這樣:
RewriteEngine On
RewriteBase /
RewriteRule ^home/(.+)\.html$ /$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.