简体   繁体   English

需要帮助编写URL重写规则正则表达式PHP和htaccess

[英]Need help writing URL Rewrite rule Regular Expression for PHP and htaccess

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. 我已经阅读了很多关于如何使用重写规则和正则表达式的网站,但它们都倾向于针对特定的例子而不会破坏语法的每一部分并实际帮助我学习这些概念以便我可以编写我需要的代码类型。

Any help is much appreciated! 任何帮助深表感谢!

I have a url like this http://www.mysite.com/myphp.php?x=1A&y=2B&c=some1&d=some2 我有这样的网址http://www.mysite.com/myphp.php?x=1A&y=2B&c=some1&d=some2

I would like this to rewrite like this: http://www.mysite.com/some1/some2 我想这样重写: 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. 这似乎很简单,但正如我所提到的,我没有遇到一个网站,以一种我可以弄清楚如何使这项工作的方式解释这些概念。

In this particular example, ?x= is always the same, while 1A varies. 在这个特定的例子中,Δx=总是相同的,而1A变化。 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. 这同样适用于php字符串的每个其他部分,但是,我假设重写规则应该处理.php文件和我实际感兴趣的变量之间的任意数量的字符。

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]

There are three parts to this: 这有三个部分:

  1. RewriteRule specifies that this is a rule for rewriting (as opposed to a condition or some other directive). RewriteRule指定这是一个重写规则(与条件或其他指令相对)。 The command is to rewrite part 2 into part 3. 该命令是将第2部分重写为第3部分。
  2. This part is a regex, and the rule will be run only if the URL matches this regex. 此部分是正则表达式,仅当URL与此正则表达式匹配时才会运行规则。 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. 括号表示括号内的部分将被存储以供将来参考。
  3. Finally, this part says to rewrite the given URL in this format. 最后,这部分说明用这种格式重写给定的URL。 $1 and $2 refer to the parts that were captured and stored (backreferences) $ 1和$ 2指的是捕获和存储的部分(反向引用)

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). 至于mod_rewrite上的高质量资源,过去我通过搜索“mod_rewrite tutorial”找到了一些(我觉得对初学者来说是个好的)。 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. 我不确定如何解释语法。

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. 一旦掌握了正则表达式的基本内容,就可以使用Python重新页面作为参考。

Something like (not tested) added to .htaccess? 是什么(未经过测试)添加到.htaccess?

RewriteEngine on
RewriteRule ^/([0-9a-z]+)/([0-9a-z]+)/$ /?c=$1&d=$2 [L]

assuming x and y are not of interest. 假设x和y不感兴趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM