htaccess:重定向到新网址问题
我想将带有可选参数的任何 php 页面重定向到新的干净网址
,例如,从: account.php?id=156
到 newurl/account/156
我使用以下重定向匹配,
RedirectMatch ^/cmstut/([a-zA-Z0-9_]+)[\.php]?[\?]?[a-zA-Z_]?[=]?([0-9]+)?$ /cmstut/redirect/newurl/$1/$2/$3 [L]
但我得到的结果是它将重定向到 newurl/account//?id=156
当我读到 htaccess 和正则表达式与 voodoo 进行比较的地方时,我觉得这很有趣:)现在我明白为什么了
I would like to redirect any php page with optional parameter to a new clean url
eg, from: account.php?id=156
to newurl/account/156
I'm using the following redirectmatch
RedirectMatch ^/cmstut/([a-zA-Z0-9_]+)[\.php]?[\?]?[a-zA-Z_]?[=]?([0-9]+)?$ /cmstut/redirect/newurl/$1/$2/$3 [L]
but the result I get is it will redirect to newurl/account//?id=156
I thought it was funny when I read somewhere where htaccess and regular expression was compared to voodoo :) we'll now I understand why
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不明白你的第三个子表达式在哪里。
$1:
([a-zA-Z0-9_]+)
$2:
([0-9]+)
$3:MIA
我可以推荐一些类似这样的东西吗?
/cmstut/([A-Za-z0-9]+)\.php(\??id=([0-9]+))?
那么您可能需要使用 3 美元才能访问 ID 号。您希望整个参数都是可选的,对吗?
I don't understand where your third subexpression is.
$1:
([a-zA-Z0-9_]+)
$2:
([0-9]+)
$3: MIA
May I recommend something more like this?
/cmstut/([A-Za-z0-9]+)\.php(\??id=([0-9]+))?
then you may have to use $3 to access the id number. you want the entire parameter to be optional, right?
我以前从未在 .htaccess 中使用过 URL 重定向,但如果它是普通的正则表达式,这应该可以工作:
由于您对自己想要的内容不是很具体,所以我进行了一些猜测。这会将
foo.php?bar=123
重定向到newurl/foo/123
并忽略bar
。编辑:想一想,从长远来看,为你重写正则表达式不会对你有帮助,而且除了你之外没有人可能确切地知道你想要什么。我认为更好的做法是向您提供正则表达式指南。 这里是一个,它专门针对 mod_rewrite。
I never used URL redirects in .htaccess before, but if it's plain regular expressions, this should work:
Since you weren't very specific in what you want, I took some guesses. This will redirect
foo.php?bar=123
tonewurl/foo/123
and will ignorebar
.Edit: Thinking about it, rewriting your regexp for you won't help you in the long term, and no one except you is likely to know exactly what you want. I think a better course of action is pointing you to a regexp guide. Here is one, and it's specifically targeted for mod_rewrite.
我终于找到了解决方案。我做了更多的研究,我使用了一种不同的方法,我相信它比我使用的方法更好
I finally found the solution. I did some more research and I used a different approach which I believe was even better then what i was using