如何向干净的 url 添加第二个可能的变量?
我已经为 URL 创建了一个 htaccess 重写代码,因此当用户访问 myurl.com/testing/
时,它会显示 index.php?page=testing
但我想有第二个或第三个页面,因此它可能看起来像 myurl.com/testing/2832/9283
并向用户显示index.php?page=testing&var1=2832&var2=9283
。
这是我当前拥有的代码:
RewriteRule ^([^\/]+)/([^\/]*)/$ index.php?page=$1&var1=$2
RewriteRule ^([^\/]+)/([^\/]*)$ index.php?page=$1&var1=$2
这有效,但我想让变量可选。如果我没有第二个变量(即只是 myurl.com/testing/
),那么它会说找不到该文件。
I have created a htaccess rewrite code for URLs so when a user goes to myurl.com/testing/
it shows them index.php?page=testing
however I would like to have a second or maybe third page so it could look like myurl.com/testing/2832/9283
and would show users index.php?page=testing&var1=2832&var2=9283
.
This is the code I currently have:
RewriteRule ^([^\/]+)/([^\/]*)/$ index.php?page=$1&var1=$2
RewriteRule ^([^\/]+)/([^\/]*)$ index.php?page=$1&var1=$2
This works but I want to make the variables optional. If I do not have a second variable (i.e. just myurl.com/testing/
) then it says it cant find the file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此规则不会触及已存在的文件和文件夹。
该规则将重写:
/help/tracking/123456/
=>/index.php?page=help&var1=tracking&var2=123456
/help/tracking
=>/index.php?page=help&var1=tracking&var2=
/help
=>/index.php?page=help&var1=&var2=
你有
page=index.php
因为你的规则重写了已经重写的 URL(很多人忘记了,当重写发生时,它会进入下一次迭代并开始再次测试所有规则)。此规则具有忽略现有文件和文件夹的条件(额外检查)。This rule will not touch already existing files and folders.
This rule will rewrite:
/help/tracking/123456/
=>/index.php?page=help&var1=tracking&var2=123456
/help/tracking
=>/index.php?page=help&var1=tracking&var2=
/help
=>/index.php?page=help&var1=&var2=
You were having
page=index.php
because your rule rewrites already rewritten URLs (A lot of people forgetting, that when rewrite happens, it goes to next iteration and starting to test all rules again). This rule has conditions (extra checks) to ignore already existing files and folders.为什么不为每种情况设置多个 RewriteRules 呢?
Why not just set multiple RewriteRules for each case?