为什么 example.com/foo/ 有效但 example.com/foo 无效?
Why example.com/foo/ works but example.com/foo doesn't?
我有一个很小的问题。我创建了一个 .htaccess 文件,假设将条件 example.com/user/foo/bar
重写为 example.com/user.php?username=&tab=
因此用户名将是 foo 并且查看选项卡必须是 bar.
但是当没有提供与 example.com/user/foo/
完全一样的条形数据时,它会使用默认选项卡转到用户(默认选项卡在 script.php 文件中生成)。
但我看到一些网站说他们可以像示例一样做到这一点。com/user/foo,没有行的 / 。当我执行 example.com/foo
转到用户时,它说找不到页面。我对应的 .htaccess 行是:
RewriteEngine On
RewriteRule ^user/(.*)/(.*)$ user.php?username=&tab= [L]
请帮我解决这个问题。
你的匹配规则是 ^user/(.*)/(.*)$
,这意味着第二个斜杠 必须 在那里(因为它被指定),但后面的文本是可选的(因为 *
匹配零个或多个字符)。
真的,我会添加两个重写规则来以不同的方式进行操作(并防止使用复杂的正则表达式),例如
RewriteRule ^user/(.*)/(.*)$ user.php?username=&tab= [L]
RewriteRule ^user/(.*) user.php?username= [L]
或者,您可以将第二个参数设为可选:
RewriteRule ^user/([^/]+)/?(?:(.*)|)$ user.php?username=&tab= [L]
只要你不介意$_GET['tab']
没有价值
我有一个很小的问题。我创建了一个 .htaccess 文件,假设将条件 example.com/user/foo/bar
重写为 example.com/user.php?username=&tab=
因此用户名将是 foo 并且查看选项卡必须是 bar.
但是当没有提供与 example.com/user/foo/
完全一样的条形数据时,它会使用默认选项卡转到用户(默认选项卡在 script.php 文件中生成)。
但我看到一些网站说他们可以像示例一样做到这一点。com/user/foo,没有行的 / 。当我执行 example.com/foo
转到用户时,它说找不到页面。我对应的 .htaccess 行是:
RewriteEngine On
RewriteRule ^user/(.*)/(.*)$ user.php?username=&tab= [L]
请帮我解决这个问题。
你的匹配规则是 ^user/(.*)/(.*)$
,这意味着第二个斜杠 必须 在那里(因为它被指定),但后面的文本是可选的(因为 *
匹配零个或多个字符)。
真的,我会添加两个重写规则来以不同的方式进行操作(并防止使用复杂的正则表达式),例如
RewriteRule ^user/(.*)/(.*)$ user.php?username=&tab= [L]
RewriteRule ^user/(.*) user.php?username= [L]
或者,您可以将第二个参数设为可选:
RewriteRule ^user/([^/]+)/?(?:(.*)|)$ user.php?username=&tab= [L]
只要你不介意$_GET['tab']