我正在尝试做一个非常简单的单个URL重写。
这就是:
RewriteRule ^blog /?post_type=post [NC,L]
我只是希望example.com/?post_type=pos
重定向并显示example.com/blog
。
我已经尝试了许多不同版本的重写,但到目前为止,我所取得的只是在example.com/blog
上没有得到404,而是一直回到example.com/?post_type=post
。
我把RewriteRule
放在了.htaccess
文件的顶部,但这并没有帮助。
这些是我在同一个.htaccess
文件中的其他重写:
#Single URL
RewriteRule ^blog ?post_type=post [NC,L]
#http www rewrite
RewriteCond %{HTTP_HOST} ^instrumentrentalbarcelona.com [NC]
RewriteRule ^(.*)$ https://www.instrumentrentalbarcelona.com/$1 [L,R=301,NC]
# 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
感谢@DocRoot帮助我。DocRoot和我在下面的评论线程中详细介绍了答案。我只是想总结一下。
- 我想显示一个包含我所有博客文章的页面
- 如果你使用WordPress的帖子类型"帖子",这可以做到。
example.com/?post_type=post
- 我想在你转到
example.com/blog
时显示该页面 -
以下"重写"可以显示所需的URL:(再次感谢@DocRoot(
RewriteCond %{QUERY_STRING} ^post_type=post$ RewriteRule ^$ /blog [QSD,R,L]
-
但
/blog
给出了404。所以 - 在WordPress中创建一个名为"博客"的页面
- 转到:设置>阅读>您的主页显示>文章页面:[选择页面"博客"]
- 保存并完成
RewriteRule ^blog /?post_type=post [NC,L]
我只想让
example.com/?post_type=pos
重定向并显示example.com/blog
您发布的指令正好相反。它从/blog<anything>
"重写"(而不是"重定向"(到/?post_type=post
要根据查询字符串进行重定向,您需要一个额外的条件来检查QUERY_STRING
服务器变量,因为RewriteRule
模式匹配的URL仅为URL路径。
请尝试以下操作:
RewriteCond %{QUERY_STRING} ^post_type=post$
RewriteRule ^$ /blog [QSD,R,L]
这将从example.com/?post_type=post
(准确地("重定向"到example.com/blog
。
QSD
标志(Apache 2.4+(是从重定向请求中删除查询字符串所必需的。如果您仍在Apache 2.2上,则需要使用?
来附加子位置,而不是使用QSD标志。例如CCD_ 21。
也不是,这是一个临时的(302(重定向。如果这应该是永久性的,那么将R
标志更改为读取R=301
-但只有在确认正常工作后。