gpt4 book ai didi

regex - .htaccess 仅当路径包含数字和字母时才重写

转载 作者:行者123 更新时间:2023-12-03 09:26:41 24 4
gpt4 key购买 nike

我使用 .htaccess 在我的网站 URL 上隐藏 GET 变量,如下所示:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Z0-9]+)$ index.php?music=$1 [NC,L]

我可以获取的唯一变量是包含字母和数字的唯一 ID。所以我想创建一个规则来检查 GET 变量是否包含字母和数字。

我想要什么:

http://www.website.com/123456ABCDEF > http://www.website.com/index.php?var=123456ABCDEF

我不想要什么:

http://www.website.com/123456 > http://www.website.com/index.php?var=123456
http://www.website.com/ABCDEF > http://www.website.com/index.php?var=ABCDEF

有什么想法吗?谢谢!

最佳答案

您可以调整正则表达式:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!(?:[A-Z]+|[0-9]+)$)[0-9A-Z]+)$ index.php?music=$1 [NC,L,QSA]

这将允许:

  • /abc123
  • /987xyz

但它不允许:

  • /你好
  • /777

更新:它基于OP的评论:

What if I want to check if one particular word is in the URL value ? For example, I want to apply this rule, only if there is the word "version" in the value.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} version [NC]
RewriteRule ^((?!(?:[A-Z]+|[0-9]+)$)[0-9A-Z]{13,})$ index.php?music=$1 [NC,L,QSA]

关于regex - .htaccess 仅当路径包含数字和字母时才重写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19524791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com