简体   繁体   English

.htaccess帮助RewriteRule

[英]help with .htaccess RewriteRule

What is the rewrite rule for following URL 后续网址的重写规则是什么

mysite.com/001234-some-keywords.html

I want to capture the 6 digit reference number something like ([0-9]{6}) 我想捕获6位数字的参考号,例如([0-9] {6})
my indexed file is 我的索引文件是

templates/default/index.php?ref=001234

One day I do a crashcourse regex.. I promise. 有一天,我会进行速成题正则表达式。

Probably 大概

RewriteRule ^([0-9]{6}).*$ templates/default/index.php?ref=$1
  • ([0-9]{6}) was already correct, it captures the six digits ([0-9]{6})已经正确,它捕获了六个数字
  • .* matches any character afterwards. .*之后匹配任何字符。
  • $1 gets replaced by the contents of the first capture group () $1被第一个捕获组()的内容替换

If you want to restrict the URL to match only if there are exactly six digits, then it should look like this (note the dash - ): 如果您想限制URL仅在正好有六位数字时才匹配,则它应如下所示(请注意破折号- ):

RewriteRule ^([0-9]{6})-.*$ templates/default/index.php?ref=$1

or matching the URL only if it is ending in .html : 或仅以.html结尾的URL匹配:

RewriteRule ^([0-9]{6})-.*?\.html$ templates/default/index.php?ref=$1

I suggest to read the mod_rewrite documentation . 我建议阅读mod_rewrite文档
Also regular-expressions.info is great for learning regular expressions. 另外, regular-expressions.info也非常适合学习正则表达式。

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

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