HTCacess重写一些URL,但将所有其他URL发送到另一个页面

htaccess rewrite some urls but send all others to another page

提问人:lewis 提问时间:11/3/2023 更新时间:11/3/2023 访问量:28

问:

这是我的.htaccess文件:

RewriteEngine On
DirectoryIndex index.php

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^login      login_reg.php [L] [NC]
RewriteRule ^(.+)       redirect.php?shortcode=$1 [L] [NC]

我想要的是将“/login”和根“/”以外的任何内容重定向到重定向.php并将request_url(斜杠后面的位)替换为简码 GET 变量。

但是,上面的文件给了我一个错误:“页面没有正确重定向”

php apache .htaccess mod-rewrite

评论

1赞 arkascha 11/3/2023
您实现了一个无休止的重写循环。考虑到该标志仅终止该运行的重写过程。因此,请改用该标志或添加另一个条件来防止请求再次重定向。LEND/redirect.php
0赞 lewis 11/3/2023
@arkascha 现在你已经解释了,谢谢。

答:

2赞 anubhava 11/3/2023 #1

几个问题:

  1. 的模式也将匹配重写loginlogin_reg.php
  2. RewriteCond仅适用于下一个,因此最后一个规则是无限重写所有内容。RewriteRule
  3. 标志需要位于单个[...]

您可以在 .htaccess 中使用以下规则:

DirectoryIndex index.php
RewriteEngine On

RewriteRule ^login/?$ login_reg.php [L,NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) redirect.php?shortcode=$1 [L,NC,QSA]

评论

1赞 lewis 11/3/2023
我想我现在明白这是如何工作的。谢谢。