用静态页面替换博客,用 htaccess 将 rest 重定向到 /blog
Replace blog with static page redirect rest to /blog with htaaccess
我有一个网站,目前有一个 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/ [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>%
尝试使用 ErrorDocument 指令
ErrorDocument 404 /blog
假设您发布的“当前 .htaccess
文件”是您打算移至 /blog/.htaccess
的 .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
substitution 字符串上的斜杠前缀。 IE。将此 RewriteRule . /index.php [L]
更改为 RewriteRule . index.php [L]
没有定义 RewriteBase
,这现在是相对于当前目录的,即。 /blog
。无需明确说明 /blog
目录。
文件末尾有一个错误的 %
?
I'd love to get anything that would normally be 404 to get redirected to the /blog
part so content doesn't get lost on the migration.
如果我们知道您原始 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
的请求重定向到 /blog/foo
。但是对 /blog/bar
的请求不会被该指令触及并正常通过 WordPress 路由(可能导致 WordPress 生成 404)。
您应该首先使用 302(临时)重定向进行测试,以避免潜在的缓存问题,并且只有在确认其按预期工作后才更改为 301(永久)重定向。
我有一个网站,目前有一个 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/ [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>%
尝试使用 ErrorDocument 指令
ErrorDocument 404 /blog
假设您发布的“当前 .htaccess
文件”是您打算移至 /blog/.htaccess
的 .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
substitution 字符串上的斜杠前缀。 IE。将此RewriteRule . /index.php [L]
更改为RewriteRule . index.php [L]
没有定义
RewriteBase
,这现在是相对于当前目录的,即。/blog
。无需明确说明/blog
目录。文件末尾有一个错误的
%
?
I'd love to get anything that would normally be 404 to get redirected to the
/blog
part so content doesn't get lost on the migration.
如果我们知道您原始 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
的请求重定向到 /blog/foo
。但是对 /blog/bar
的请求不会被该指令触及并正常通过 WordPress 路由(可能导致 WordPress 生成 404)。
您应该首先使用 302(临时)重定向进行测试,以避免潜在的缓存问题,并且只有在确认其按预期工作后才更改为 301(永久)重定向。