首页 > 解决方案 > 将博客替换为静态页面,将其余部分重定向到 /blog 与 htaac​​cess

问题描述

我有一个网站,目前有一个 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>%

标签: .htaccessredirect

解决方案


假设您发布的“当前.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]

/foo以上将重定向对to的请求/blog/foo/blog/bar但是这个指令不会触及请求,并像往常一样通过 WordPress 路由(可能导致 WordPress 生成 404)。

您应该首先使用 302(临时)重定向进行测试以避免潜在的缓存问题,并且只有在确认它按预期工作后才更改为 301(永久)重定向。


推荐阅读