简体   繁体   English

.htaccess RedirectMatch语法

[英].htaccess RedirectMatch Syntax

I am having a heck of a time trying to get the syntax correct on a 301 redirect. 我有一段时间想在301重定向上获取正确的语法。 I've researched online quite a bit but as I am completely new to this it just isn't clicking for me. 我已经在网上进行了很多研究,但是由于这是我的新手,所以这并不是我想要的。 I hope someone here can help. 我希望这里有人可以提供帮助。

I'm typing to write a rule that will change all links using the following format: 我正在输入一条规则,该规则将使用以下格式更改所有链接:

http://www.example.com/yyyy/mm/dd/page-name http://www.example.com/yyyy/mm/dd/page-name

To

http://www.example.com/category/page-name http://www.example.com/category/page-name

In the above example, "/yyyy/mm/dd/" are variable for any number of dates. 在上面的示例中,“ / yyyy / mm / dd /”对于任意数量的日期都是可变的。 It's a Wordpress migration and the new theme does not allow us to preserve the day and name format for permalinks, we are trying to preserve several years' worth of SEO "link juice." 这是Wordpress的迁移,新主题不允许我们保留永久链接的日期和名称格式,我们正在尝试保留数年的SEO“链接汁”。

For various reasons I would prefer to do this using a 301 Redirect instead of as a Rewrite. 由于各种原因,我宁愿使用301重定向而不是重写来执行此操作。 Thanks in advance! 提前致谢!

EDIT to add: Here is my attempt at it, I think I have mixed syntax for rewrites and redirects (among other things): 编辑要添加:这是我的尝试,我认为我混合使用了重写和重定向的语法(以及其他方法):

# 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

RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$ /category/$4/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$ /category/$4 [L,R=301,NC]

I appear to have gotten it working with this: 我似乎已经使用它:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RedirectMatch 301 ^/[0-9]{4}/[0-9]{2}/[0-9]{2}/(.*)$ http://www.example.com/category/$1/
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Thanks to everyone who replied, it helped me narrow things down at least. 感谢所有回答,这至少帮助我缩小了范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM