apache - mod_rewrite 不重写

标签 apache .htaccess mod-rewrite

我有以下代码:

RewriteEngine on
RewriteRule ^user/([a-z]+)$ index.php?Agent=$1 [QSA]

理论上,任何使用以下格式访问我网站的访问者:

www.site.com/user/username

应定向至:

www.site.com/index.php?Agent=username

它们被视为/user 是一个真实的目录,并且/user/username 是一个有效的文件并且存在(但事实并非如此)。有什么想法吗?

编辑#1

我稍微修改了我的 htaccess,甚至去掉了/user 要求,以下 .htaccess 文件会导致 500 错误,即使单独访问主域 (www.site.com) 时也是如此:

ErrorDocument 404 /404.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^(.+)$ index.php?Agent=$1 [QSA]

最佳答案

they are instead being treated as if /user is a real directory and as if /user/username is a valid file and exists (which it does not).

你这样说到底是什么意思?它显示什么?

您要重写还是**重定向?

重写=当用户输入www.site.com/user/username时调用www.site.com/index.php?Agent=username

Redirect = 将用户重定向到另一个 URL,以便他们的地址字段显示 www.site.com/index.php?Agent=username

现在,您的代码应该重写,而不是重定向,这可能就是您想要的。只是检查...

您的问题可能是您正在测试的用户名包含大写字母。添加 NC 使匹配不区分大小写。

RewriteRule ^user/([a-z]+)$ index.php?Agent=$1 [QSA,NC]

如果您希望允许使用字母 a-z 以外的其他字符,您可以向字符类添加更多字符,或者只编写 (.+) 来匹配任何字符。

编辑

根据下面的问题,我认为发生无限重定向是因为规则被再次触发,因为重写与新的 index.php 请求匹配。只需检查重定向的目标不是文件就可以解决这个问题。 (-f =“是文件”。!=否定条件。)

RewriteCond %{REQUEST_FILENAME} !-f

关于apache - mod_rewrite 不重写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5086060/

相关文章:

apache - Htaccess mod 重写不起作用

apache - 无法启用 php display_errors

.htaccess - 如何在 GoDaddy Linux 主机上使用 .htaccess 文件更改 PHP 文件扩展名?

apache - RewriteCond内部的%N个反向引用

Apache mod_rewrite 使用 302 而不是 https 重定向到 http

.htaccess - 仅使用 .htaccess 让 Symfony2 在共享主机上工作

java - 无法使用多线程 PDFTextStripper 读取单个页面

html - 通过 apache 服务器查看时,我的 django 管理页面没有 css

php - "Your PHP installation appears to be missing the MySQL extension which is required by WordPress."

apache - HTAccess : Rewrite Subdomain to HTTPS Main site