htaccess:重定向到新网址问题

发布于 2024-09-26 01:26:54 字数 401 浏览 0 评论 0原文

我想将带有可选参数的任何 php 页面重定向到新的干净网址

,例如,从: account.php?id=156newurl/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

囍孤女 2024-10-03 01:26:54

我不明白你的第三个子表达式在哪里。

$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?

丘比特射中我 2024-10-03 01:26:54

我以前从未在 .htaccess 中使用过 URL 重定向,但如果它是普通的正则表达式,这应该可以工作:

RedirectMatch ^/cmstut/([a-zA-Z0-9_]+)\.php(\?[a-zA-Z_]+=([0-9]+))?$ /cmstut/redirect/newurl/$1/$3 [L]

由于您对自己想要的内容不是很具体,所以我进行了一些猜测。这会将 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:

RedirectMatch ^/cmstut/([a-zA-Z0-9_]+)\.php(\?[a-zA-Z_]+=([0-9]+))?$ /cmstut/redirect/newurl/$1/$3 [L]

Since you weren't very specific in what you want, I took some guesses. This will redirect foo.php?bar=123 to newurl/foo/123 and will ignore bar.


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.

鲸落 2024-10-03 01:26:54

我终于找到了解决方案。我做了更多的研究,我使用了一种不同的方法,我相信它比我使用的方法更好

RewriteCond %{QUERY_STRING} ^id=([0-9]+)$ [NC]
RewriteRule ^([A-Za-z0-9]+)\.php$ /cmstut/redirect/newurl/account/%1? [R=302,L] 

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

RewriteCond %{QUERY_STRING} ^id=([0-9]+)$ [NC]
RewriteRule ^([A-Za-z0-9]+)\.php$ /cmstut/redirect/newurl/account/%1? [R=302,L] 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文