如何向干净的 url 添加第二个可能的变量?

发布于 2024-11-26 00:12:09 字数 507 浏览 0 评论 0原文

我已经为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提赋 2024-12-03 00:12:10
# 3-level deep parameters
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)(/([^/]+))?(/([^/]+))?(/)?$ /index.php?page=$1&var1=$3&var2=$5 [QSA,L]
  1. 此规则不会触及已存在的文件和文件夹。

  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(很多人忘记了,当重写发生时,它会进入下一次迭代并开始再次测试所有规则)。此规则具有忽略现有文件和文件夹的条件(额外检查)。

# 3-level deep parameters
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)(/([^/]+))?(/([^/]+))?(/)?$ /index.php?page=$1&var1=$3&var2=$5 [QSA,L]
  1. This rule will not touch already existing files and folders.

  2. 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.

幽蝶幻影 2024-12-03 00:12:10

为什么不为每种情况设置多个 RewriteRules 呢?

RewriteRule ^([^/]+)/?$ index.php?page=$1
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?page=$1&var1=$2
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?page=$1&var1=$2&var2=$3

Why not just set multiple RewriteRules for each case?

RewriteRule ^([^/]+)/?$ index.php?page=$1
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?page=$1&var1=$2
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?page=$1&var1=$2&var2=$3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文