.htaccess htaccess重写如果url没有查询字符串

lfapxunr  于 2022-11-16  发布在  其他
关注(0)|答案(2)|浏览(233)

我有一个htaccess,如果条件不满足,它会重写到**/login**。
但是,我不知道如何将**/reset?query_string包括到接受的条件中。并且希望排除/reset**

**/重置----------

RewriteEngine On
RewriteBase /index.php

RewriteCond %{REQUEST_URI} !^/login$
RewriteCond %{REQUEST_URI} !^/forgotten$

RewriteRule ^([^\.]+)$ /login [R=301,L,QSD]
RewriteRule ^(/)?$ /login [R=301,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
wbgh16ku

wbgh16ku1#

下面是.htaccess的一个修改版本,其中不包括/reset?query

RewriteEngine On

# /reset with query string
RewriteCond %{QUERY_STRING} .
RewriteRule ^(reset)/?$ $1.php [L,NC]

RewriteCond %{REQUEST_URI} !^/(forgotten|login)$ [NC]
RewriteRule ^([^.]*)$ /login [R=301,L,QSD]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)$ $1.php [L]

请确保在清除浏览器缓存后对其进行测试。

vmdwslir

vmdwslir2#

使用您显示示例和尝试,请尝试以下htaccess规则文件请确保在测试URL之前清除浏览器缓存
我还使用了apache的变量THE_REQUEST,通过它我们可以处理uri和查询字符串。

RewriteEngine ON
RewriteBase /
##Rules for handling rest uri without query string.
RewriteCond %{QUERY_STRING} ^$ 
RewriteRule ^reset/?$ /login? [R=301,L,NC]

##Rules for reset with uri and query string.
RewriteCond %{THE_REQUEST} \s/reset\?\S+\s [NC]
RewriteRule ^ /reset? [R=301,NC]

##Rule for login page's backend rewrite.
RewriteRule ^login/?$ login.php [QSA,NC,L]

##Rule for forgotten page's backend rewrite.
RewriteRule ^forgotten/?$ forgotten.php [QSA,NC,L]

##Rules for non-existing pages should be served with index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

相关问题