.htaccess - 将博客替换为静态页面,将其余部分重定向到 /blog 与 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/$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>%
解决方案
假设您发布的“当前.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(永久)重定向。
推荐阅读
- python - python中的相对导入与本地ang全局库
- express - 如何使用在第一次注销时有效但在后续注销时无效的护照 saml 修复 SLO
- python-3.x - 查找给定列表的重叠
- c++ - 如何访问 C 文件中的 C++ public bool 类成员(以验证)?
- python - matplotlib 弹出窗口中的错误(AttributeError:'NoneType' 对象没有属性'set_canvas')
- python-3.x - 将获取请求从树莓派重新发送到烧瓶应用程序时阻止输入
- angular - Ionic 4中的动态进度条?
- c# - 在 MVC 中追加重复的 div 时不出现下拉列表
- python - 使用 PyInstaller 将 Cython 编译的模块和 python 代码构建为可执行二进制文件
- android - 根据方位旋转谷歌地图,中心有固定标记