該賞金到期的明天。這個問題的答案有資格獲得 50聲望獎勵。 安東尼奧·拉古納(Antonio Laguna)希望引起對這個問題的更多關注。
我有一個網站,目前有一個 WordPress 博客。我想將博客移入/blog并使靜態頁面成為新的“根”。
就 FTP 而言,這很容易,只需移動檔案夾即可。
但是,我希望將通常為 404 的任何/blog內容重定向到該部分,以便內容不會在遷移時丟失。
這是可行的.hatccess嗎?
當前.htaccess檔案:
#DirectoryIndex index.php index.html
#Options FollowSymLinks
# Indexes
Options All -Indexes
# REDIRECT https
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
# BEGIN WordPress
# Dynamically generated by WP
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# BEGIN FileETag MTime Size
<ifmodule mod_expires.c>
<filesmatch "\.(jpg|gif|png|css|js)$">
ExpiresActive on
ExpiresDefault "access plus 1 year"
</filesmatch>
</ifmodule>
# END FileETag MTime Size<!--formatted-->
# Protecting htaccess
<files .htaccess>
order allow,deny
deny from all
</files>
# Protecting wpconfig.php
<files wp-config.php>
order allow,deny
deny from all
</files>%
uj5u.com熱心網友回復:
嘗試使用 ErrorDocument 指令
ErrorDocument 404 /blog
uj5u.com熱心網友回復:
假設.htaccess您發布的“當前檔案”是.htaccess您打算移動到的檔案/blog/.htaccess。在這種情況下,您需要按如下方式更改它:
#DirectoryIndex index.php index.html
#Options FollowSymLinks
# Indexes
Options All -Indexes
# REDIRECT https
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]
# BEGIN WordPress
# Dynamically generated by WP
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
# END WordPress
# BEGIN FileETag MTime Size
<ifmodule mod_expires.c>
<filesmatch "\.(jpg|gif|png|css|js)$">
ExpiresActive on
ExpiresDefault "access plus 1 year"
</filesmatch>
</ifmodule>
# END FileETag MTime Size<!--formatted-->
# Protecting htaccess
<files .htaccess>
order allow,deny
deny from all
</files>
# Protecting wpconfig.php
<files wp-config.php>
order allow,deny
deny from all
</files>
顯著變化:
將 HTTP 更改為 HTTPS 重定向以使用
REQUEST_URI服務器變數而不是反向參考。由于它位于子目錄 (blog) 中,否則該子目錄將從重定向到 HTTPS 中省略。注釋掉(洗掉)
RewriteBase指令。這在此處不是必需的,但如果您確實需要設定它,則應將其設定為RewriteBase /blog.洗掉了
RewriteRule替換字串上的斜杠前綴。IE。RewriteRule . /index.php [L]把這個改成這個RewriteRule . index.php [L]沒有
RewriteBase定義,這現在是相對于當前目錄的,即。/blog. 無需明確說明/blog目錄。您
%在檔案末尾有錯誤嗎?
我很想獲得通常為 404 的任何內容以重定向到該
/blog部分,以便內容不會在遷移時丟失。
如果我們知道您的原始 URL 的格式,這也許可以改進,但是,我們基本上需要將任何請求重定向到/blog子目錄之外的任何地方- 這將觸發 404 - 重定向到/blog子目錄。
您需要.htaccess使用以下內容在檔案根目錄中創建一個新檔案:
RewriteEngine On
# Redirect any 404s back to the "/blog" directory
RewriteRule %{REQUEST_FILENAME} !-f
RewriteRule %{REQUEST_FILENAME} !-d
RewriteRule !^blog/ /blog%{REQUEST_URI} [R=301,L]
以上將重定向對/footo的請求/blog/foo。但是請求/blog/bar不會被這個指令觸及并通過 WordPress 正常路由(可能導致 WordPress 生成 404)。
您應該首先使用 302(臨時)重定向進行測驗以避免潛在的快取問題,并且只有在確認它按預期作業后才更改為 301(永久)重定向。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/335958.html
