.htaccess重定向循环,添加多个.php扩展

.htaccess重定向循环,添加多个.php扩展,php,.htaccess,mod-rewrite,Php,.htaccess,Mod Rewrite,我为学校建立了一个小型的家长/教师社交网络。我使用我的.htaccess文件使指向教师档案的链接更清晰,并在所有URL中添加一个尾随斜杠。当我转到/teachers/teacher name/链接(有时)重定向到/teachers/teacher name.php.php.php.php.php.php.php时,我遇到了这个问题。下面是我的.htaccess文件。有时,如果我在Chrome中清除浏览器缓存,它会暂时修复它。我不太懂wright.htaccess语法,但我对它非常熟悉。任何建议都

我为学校建立了一个小型的家长/教师社交网络。我使用我的
.htaccess
文件使指向教师档案的链接更清晰,并在所有URL中添加一个尾随斜杠。当我转到
/teachers/teacher name/
链接(有时)重定向到
/teachers/teacher name.php.php.php.php.php.php.php时,我遇到了这个问题。
下面是我的
.htaccess
文件。有时,如果我在Chrome中清除浏览器缓存,它会暂时修复它。我不太懂wright
.htaccess
语法,但我对它非常熟悉。任何建议都将不胜感激

RewriteEngine on
RewriteBase /

#remove php ext
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ $1/$2.php

#force trailing slash/
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)([^/])$    /$1$2/ [L,R=301]

#other rewrites
RewriteRule ^teachers/([^/\.]+)/$ /teachers/profile.php?u=$1
RewriteRule ^teachers/([^/\.]+)/posts/$ /teachers/posts.php?u=$1
RewriteRule ^teachers/([^/\.]+)/posts/([^/\.]+)/$ /teachers/post.php?u=$1&p=$2

RewriteRule ^gallery/([^/\.]+)/$ /gallery/album.php?n=$1
RewriteRule ^gallery/([^/\.]+)/slideshow/$ /gallery/slideshow.php?n=$1
RewriteRule ^gallery/([^/\.]+)/([^/\.]+)/([^/\.]+)/$ /gallery/photo.php?a=$1&p=$2&e=$3
编辑:我附上了一张截图,正好是我所说的内容。

在.php规则末尾添加一个
[L]
,以确保mod_rewrite停止处理


通常,当您找到与规则匹配的类型时,它是您希望根据请求运行的唯一规则。您的最后六条规则也是这方面的例子,但您可以不使用它们,因为它们彼此不重叠。

这应该是因为您决定添加尾随斜杠:

#force trailing slash/
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)([^/])$    /$1$2/ [L,R=301]
您可以写以下内容来代替此内容:

#force trailing slash/
RewriteCond %{REQUEST_FILENAME} !-f
#the following rule only works for .php obviously, so add other rules
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^(.*)([^/])$    /$1$2/ [L,R=301]

由于您使用了301重定向,浏览器很可能会缓存重定向,因此您可能需要在浏览器缓存正常工作之前清除它。

再次查看此信息,我看到了另一个错误。你显然有两条规则来分享条件。这是行不通的。每个规则都必须有自己的条件

#remove php ext
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ $1/$2.php #this rule will always run
应改为

#remove php ext
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/([^/]+)/$ $1/$2.php #this rule will always run
除此之外,您是否正在尝试将
.php
添加到以
/
结尾的URL

RewriteRule ^([^/]+)/$ $1.php

下面你忘记了你的
.htaccess
你忘记了添加
.htaccess
文件具体来说,我指的是前两个,但其余的都是可选的,因为它们是互斥的。仍然得到相同的结果:/“在.php规则的末尾添加一个[L],以确保mod_rewrite停止处理。”---这是不正确的
[五十] 
标志仅表示“本轮重写过程已完成”,并且重写过程将使用新URL重新启动。仅当新URL再次在引擎中运行时,如使用
[R]
用于重定向。当然,您可以在.php规则中添加一个
RewriteCond
,以确保URL不以.php结尾