通过 .htaccess 重定向多个 URL [英] redirect multiple URLs through .htaccess

查看:39
本文介绍了通过 .htaccess 重定向多个 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多具有类似网址的页面,例如:

I have many pages with similar urls like:

https://example.com/mainpage/1
https://example.com/mainpage/2
https://example.com/mainpage/3
.
..
https://example.com/mainpage/100

我想使用 .htaccess 将它们重定向到新的单个页面.喜欢:

I want to redirect them using .htaccess to new individual pages. Like:

  • https://example.com/mainpage/1 应该重定向到 https://example.com/mainpage-1
  • https://example.com/mainpage/2 应该重定向到 https://example.com/mainpage-2
  • https://example.com/mainpage/3 应该重定向到 https://example.com/mainpage-3
  • https://example.com/mainpage/1 should redirect to https://example.com/mainpage-1
  • https://example.com/mainpage/2 should redirect to https://example.com/mainpage-2
  • https://example.com/mainpage/3 should redirect to https://example.com/mainpage-3

等等.

因为有很多页面非常相似的网址,我想知道每种代码是否可以工作.

since there are many pages of very similar URLs, I want to know if a single code of each kind can work.

附言: 我对编码知之甚少.我的个人博客需要帮助

P.S. : I have little knowledge of coding. Need help for my personal blog

推荐答案

您能否尝试使用所示示例进行以下操作、编写和测试.请在测试 URL 之前清除浏览器缓存.第一个特定于显示的示例,将主页字符串保持在第一个反向引用中,并将 / 之后的其余数字保留在第二个反向引用中,我们在其右侧部分重定向时使用它们.

Could you please try following, written and tested with shown samples. Please clear your browser cache before you test your URLs. 1st one is specific to shown samples, keeping mainpage string into 1st backreference and rest digits after / in 2nd backreference which we are using them while redirecting on its right side part.

特定于样本:

RewriteEngine ON
RewriteRule ^(mainpage)/([0-9]+)/?$ $1-$2 [R=302,NC,L]

通用:

RewriteEngine ON
RewriteRule ^([\w-]+)/([0-9]+)/?$ $1-$2 [R=302,L]

这篇关于通过 .htaccess 重定向多个 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆