需要帮助编写URL重写规则定期防爆pression PHP和htaccess的重写、规则、URL、htaccess

2023-09-02 00:59:04 作者:悟空保护我

我读了很多网站在谈论如何使用重写规则和正前pressions,但他们都往往是具体的例子,而不打破每一块的语法,实际上帮助我学习概念,让我可以写code,我需要的类型。

I've read a lot of sites talking about how to use Rewrite Rules and Regular Expressions, but they all tend to be for specific examples without breaking down every piece of the syntax and actually helping me to learn the concepts so that I can write the type of code I need.

任何帮助是非常AP preciated!

Any help is much appreciated!

我有这样的 http://www.mysite.com/myphp.php?x=1A&y=2B&c=some1&d=some2

我想这个改写这样的: http://www.mysite.com/some1/some2

I would like this to rewrite like this: http://www.mysite.com/some1/some2

这似乎很简单,但正如我所说,我还没有碰到一个网站解释的方式的概念,我可以计算出如何使这项工作。

It seems really simple, but as I mentioned, I haven't run into a site explaining the concepts in a way that I could figure out how to make this work.

在此特定示例中,X =始终是相同的,而图1A而变化。这同样适用于其他各件PHP字符串的,但是,我假设重写规则应该处理任何数量的.php文件和变量,我真正感兴趣的人物。

In this particular example, ?x= is always the same, while 1A varies. The same applies to each of the other pieces of the php string, however, I'm assuming the rewrite rule should handle any number of characters between the .php file and the variables I'm actually interested in.

再次感谢的人谁可以帮助解释此或提供两个重写规则和规则的前pressions优质资源,以帮助我学习吧。

Thanks again to anyone who can help explain this or provide a high quality resource on both rewrite rules and regular expressions to help me learn it.

推荐答案

像这样的规则就可以了(测试):

A rule like this will work (tested):

RewriteRule ^([^/]+)/([^/]+) myphp.php?x=1A&y=2B&c=$1&d=$2 [L]

有三部分内容:

重写规则规定,这是改写规则(而不是一个条件或其他指令)。该命令重写部分2到3部分。 在这部分是一个正则表达式,且仅当URL这正则表达式匹配的规则将被运行。在这种情况下,我们说 - 查找字符串的开头,然后一帮非斜线字符,然后斜线,然后又一堆非斜杠。括号意味括号内的零件将被储存以备将来参考。 在最后,这部分说要重写指定的URL这种格式。 $ 1和$ 2是指被捕获和存储的部件(反向引用) RewriteRule specifies that this is a rule for rewriting (as opposed to a condition or some other directive). The command is to rewrite part 2 into part 3. This part is a regex, and the rule will be run only if the URL matches this regex. In this case, we are saying - look for the beginning of the string, then a bunch of non-slash characters, then a slash, then another bunch of non-slash characters. The parentheses mean the parts within the parentheses will be stored for future reference. Finally, this part says to rewrite the given URL in this format. $1 and $2 refer to the parts that were captured and stored (backreferences)

至于对mod_rewrite的优质资源,在过去,我发现了一些通过搜索mod_rewrite的教程(我认为的这是一个很好的初学者)。他们会大多是这样的,所以如果有什么具体的你不明白,拿到闪开前进了。我不知道怎么回事的语法可以解释的。

As for high-quality resources on mod_rewrite, in the past I've found a few by searching for "mod_rewrite tutorial" (I thought this was a good one for a beginner). They'll mostly be like that, so if there's anything specific you don't understand, get that out of the way before moving forward. I'm not sure how else the syntax can be explained.

的正则表达式是一个不同的事情,再次,很多资源在那里 - 包括适合初学者。一旦你的正则表达式的基本窍门,您可以使用 Python的重页面作为参考

Regexes are a different thing, again, many resources out there - including for beginners. Once you get the basic hang of regexes, you can use the Python re page as a reference.