使用.htaccess和Regex替换部分网址路径和查询字符串? [英] Replace portions of a url path and query string using .htaccess and Regex?

查看:46
本文介绍了使用.htaccess和Regex替换部分网址路径和查询字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何替换:

https://www.example.com/index.php?option=com_k2&view=item&id=7377

通过:

https://www.portal-gestao.com/artigos/7377

我尝试过这个:

RewriteRule ^index.php?option=com_k2&view=item&id=(.*)$ /artigos/$1 [R=301,L]

这:

RewriteRule ^index.php?option=com_k2&view=item&id=\/([0-9]{4}-.*)$ /artigos/$1 [NC,R,L]

推荐答案

您无法在RewriteRule中捕获查询字符串.请改用RewriteCond.在您的.htaccess顶部使用此规则:

You cannot capture query string in RewriteRule. Use a RewriteCond instead. Use this rule at top of your .htaccess:

RewriteCond %{THE_REQUEST} /index\.php\?option=com_k2&view=item&id=([^\s&]+) [NC]
RewriteRule ^ https://www.portal-gestao.com/artigos/%1? [L,R=302]

这篇关于使用.htaccess和Regex替换部分网址路径和查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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