.htaccess 301从旧重写到新的重定向
我在进行301重定向方面有问题。我在网站上使用重写:
domain.com/rubric-news
使用“平面”链接系统
domain.com/article-815/news-title
RewriteRule ^rubric-([^*]*) news.php?kat=$1
RewriteRule ^article-([^*]*)/([^*]*)-([^*]*) article.php?id=$1&kat=$2&title=$3
今天,我开始为SEO构建更好的内部链接系统,因此现在重写如下:
domain.com/news
domain.com/news/title-815
RewriteRule ^([a-z0-9]+)$ news.php?kat=$1 [L]
RewriteRule ^([^*]*)/([^*]*)-([^*]*) article.php?id=$3&kat=$1&title=$2 [L,NC,QSA]
现在,我应该从旧链接重定向到新链接,我不知道该如何制作。谁能帮我吗?)
I have a problem with making a 301 redirection. I was using "flat" link system on website with RewriteRule:
domain.com/rubric-news
domain.com/article-815/news-title
RewriteRule ^rubric-([^*]*) news.php?kat=$1
RewriteRule ^article-([^*]*)/([^*]*)-([^*]*) article.php?id=$1&kat=$2&title=$3
Today I started to build better internal links system for SEO so now RewriteRules looks like this:
domain.com/news
domain.com/news/title-815
RewriteRule ^([a-z0-9]+)$ news.php?kat=$1 [L]
RewriteRule ^([^*]*)/([^*]*)-([^*]*) article.php?id=$3&kat=$1&title=$2 [L,NC,QSA]
now I should make 301 redirection from old links to the new ones and I don't know how to make it. Can anyone help me?))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
重定向指令将遵循与重写相同的原则。 (尽管您当前正在使用的重写/正则表达式存在潜在的问题 - 请参见下文)。
在您现有的重写之前尝试这样的尝试:
我假设您的“ ID”参数都是数字(按照您的示例)。 (尽管您现有的指令不执行此功能。)
默认情况下将通过任何查询字符串(您不需要
QSA
flag在此处)。您应该首先使用302(临时)重定向测试,以避免潜在的缓存问题。测试前清除浏览器缓存。
旁边:
REGEX
[^*]*
匹配任何不是星号(*
)0或更多次的事物。您应该匹配任何不斜线1或更多次的东西,即。[^/]+
。而且您缺少正则末端的串线锚(如先前的规则所示)。使用您当前正在使用的非常通用的正则(即。前面的规则)。如果您的 id 仅由数字组成,则仅匹配数字。如果 title 仅是字母数字,则仅匹配字母和数字,而不是所有内容。当前它看起来“看起来” news 和 title 参数可能包含连字符,但这会引入歧义。
NC
标志在这里自然是多余的。而是考虑这样的东西:
如果您的 title 可以包含连字符,则应相应地更改正则正则。例如。
^(\ w+)/([\ w-]+) - (\ d+)$
The redirect directives would follow exactly the same principles as you have already used for the rewrites. (Although there are potential issues with the rewrites/regex you are currently using - see below).
Try it like this before your existing rewrites:
I have assumed your "id" parameter is all numeric (as per your example). (Although your existing directives do not enforce this.)
Any query string will be be passed through by default (you do not need the
QSA
flag here).You should test first with 302 (temporary) redirects to avoid potential caching issues. Clear your browser cache before testing.
Aside:
The regex
[^*]*
matches anything that is not an asterisk (*
) 0 or more times. You should be matching anything that is not a slash 1 or more times, ie.[^/]+
. And you are missing the end-of-string anchor on the end of the regex (as you have on the preceding rule). With the very generic regex you are currently using (ie.[^*]*
) this does not strictly matter, however, it is potentially ambiguous, ...Your regex should be more restrictive (similar to the preceding rule). If your id consists of digits only then match only digits. If the title is only alphanumeric then match only letters and numbers, not everything. Currentrly it "looks like" the news and title parameters could contain hyphens, but that would introduce an ambiguity.
The
NC
flag is naturally superfluous here.Consider something like this instead:
If your title can contain hyphens then change the regex accordingly. eg.
^(\w+)/([\w-]+)-(\d+)$