【问题标题】:RewriteRules in htaccesshtaccess 中的重写规则
【发布时间】:2023-03-25 18:44:01
【问题描述】:

我在 .htaceess 中有这段代码:

Options +FollowSymlinks 
RewriteEngine On
RewriteBase /
RewriteRule aa/(.+)  $1 [QSA,L]
RewriteRule .+  index.php?username=$0 [L,QSA]

index.php 内容:

<?php
var_dump($_GET);
?>

这是文件的结构:

root
|
|_____css
|      |____ style.css
|
|_____ .htaccess
|_____ index.php

我希望当类型 http://domain/aa/css/style.css 重定向到 /css/style.css 对于我输入的所有其他网址,即:http://domain/test 重定向到 /index.php?username=test

现在当我输入 http://domain/test 时,它的效果很好。但是当我输入 http://domain/aa/css/style.css 而不是 style.css 时,浏览器会显示:

array(1) { ["username"]=> string(13) "css/style.css" }

现在如何将 .htaccess 更改为一切正常?

【问题讨论】:

  • 在第二个重写规则之前添加 RewriteCond : RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .+ index.php?username=$0 [L,QSA]

标签: .htaccess mod-rewrite


【解决方案1】:

你可以使用:(见下面的 cmets)

Options +FollowSymlinks 
RewriteEngine On
RewriteBase /

RewriteRule aa/(.+) $1 [NC,L]

RewriteCond %{THE_REQUEST} !/aa/ [NC]
RewriteRule .+ index.php?username=$0 [L,QSA]

【讨论】:

  • 现在我想当用户转到 http://domain/css/style.css 时,即使该目录和文件存在 htaccess 将其重写为 http://domain/index.php?username=css/style.css 并且仅在转到 http://domain/aa/css/style.css 时重写它到http://domain/css/style.css。现在请指导我。
  • 好的,我删除了两个 rewritecond 语句,但是这次当我在第一个 rewriterule 语句匹配后转到 http://domain/aa/css/style.css 时,[L] 开关不要停止重写并将输出发送到第二个 rewriterule 和最终输出 url 是 http://domain/index.php?username=css/style.css 。但为什么??
  • 好的,我改变了它并且工作正常thanx。但现在我想知道 [L] flag 是什么。在匹配后的第一个重写语句中,[L] 标志必须停止重写过程。但没有这样做!为什么?
  • L 不会停止遵守规则。它只会停止当前规则。
猜你喜欢
  • 2011-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多