我们希望将博客文件夹重定向到新闻文件夹。例如:任何人访问..。
https://example.com/blog/blog-title
重定向到https://example.com/news/blog-title
https://example.com/news-blog-presenters
重定向到https://example.com/blog
.下面是我的HTACCESS代码。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.co.uk [NC]
RewriteRule ^(.*)$ https://example.co.uk/$1 [L,R=301]
</IfModule>
# BEGIN WordPress
# The directives (lines) between `BEGIN WordPress` and `END WordPress` are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/news-blog-speaker/?$ [NC]
RewriteRule ^/blog/?$ /news/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
发布于 2020-09-30 01:23:01
RewriteCond %{REQUEST_URI}!^/新闻-博客-扬声器/?$ NC RewriteRule ^/blog/$ /news/$1 R=301,L
上述指令有几个问题:
.htaccess
由RewriteRule
模式匹配的URL路径不以斜杠开头。所以,它应该是^/blog
.^blog
,它只是试图匹配/blog
或/blog/
。但是您需要重定向description./blog/<blog-title>
中所述,您没有捕获RewriteRule
模式中的任何内容,$1
反向引用始终是空的。您需要从请求的URL中捕获<blog-title>
,但如上所述,您没有尝试这样做。检查请求的RewriteCond
指令)并不是/news-blog-speaker
,因为您已经在检查请求是否为/blog
。不可能两者兼而有之。# BEGIN/END WordPress
注释标记之间的代码.在# BEGIN WordPress
部分之前尝试以下操作:
# Redirect "/blog/<blog-title>" to "/news/<blog-title>"
RewriteRule ^blog/([^/]+)/?$ /news/$1 [R=302,L]
# Redirect "/news-blog-presenters" to "/blog"
RewriteRule ^news-blog-presenters/?$ /blog [R=302,L]
通过匹配一个非空的<blog-title>
而不是简单的/blog/
来避免“冲突”。根据您声明的需求,这似乎是您所需要的。
我假设/blog
不是物理目录,否则对/blog
的请求(没有尾随斜杠)将触发重定向到/blog/
(由mod_dir)。
首先用302 (临时)重定向进行测试,如果需要的话,只更改为301 (永久)--在确认重定向工作之后。
在测试之前,您可能需要清除浏览器缓存。
https://stackoverflow.com/questions/64123923
复制相似问题