Regex .htaccess重写时出现了一些问题

Regex .htaccess重写时出现了一些问题,regex,.htaccess,mod-rewrite,redirect,rewrite,Regex,.htaccess,Mod Rewrite,Redirect,Rewrite,我对当前的htaccess文件重写有一些问题,如果有人能帮我解决,我将不胜感激 以下是我当前的htaccess文件: Options +FollowSymLinks -MultiViews -indexes RewriteEngine On RewriteBase / # remove .php; use THE_REQUEST to prevent infinite loops RewriteCond %{THE_REQUEST} ^GET\

我对当前的htaccess文件重写有一些问题,如果有人能帮我解决,我将不胜感激

以下是我当前的htaccess文件:

     Options +FollowSymLinks -MultiViews -indexes
     RewriteEngine On
     RewriteBase /
     # remove .php; use THE_REQUEST to prevent infinite loops
     RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
     RewriteRule (.*)\.php$ $1 [R=301]
     # remove index
     RewriteRule (.*)/index$ $1/ [R=301]
     # remove slash if not directory
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteCond %{REQUEST_URI} /$
     RewriteRule (.*)/ $1 [R=301]
     # add .php to access file, but don't redirect
     RewriteCond %{REQUEST_FILENAME}.php -f
     RewriteCond %{REQUEST_URI} !/$
     RewriteRule (.*) $1\.php [L]        
     RewriteCond %{HTTP_HOST} ^domain.com [NC]
     RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
     RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+sections/([^\s]+) [NC]
     RewriteRule ^ %1 [R=301,L]
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteRule (?!^sections/)^(.*)$ /sections/$1 [L,NC]
在我的public_html文件夹中,我有一个名为sections的子文件夹,它本身包含几个其他子文件夹。我希望实现的是使用我上面发布的配置来隐藏/sections/文件夹,该文件夹在一定程度上可以工作,但是我仍然遇到一些问题

如果我要打开www.mydomain.com/sections/file,它会成功重定向到www.mydomain.com/file-这已经很好了,但是如果我尝试添加.php扩展名只是为了测试,则会跳过redirect works sections文件夹,然而,文件扩展名没有被修剪,我仍然看到.php应该被删除,我看到一个文本永久移动的页面-文档已经移动到这里

在我当前的.htaccess文件中,是否有任何我可以做/修改的事情或不正确的事情?如有专家建议,将不胜感激

多谢各位

根据要求更新:

     DirectoryIndex index.php index.html
     Options +FollowSymLinks -MultiViews -indexes
     RewriteEngine On
     RewriteBase /

     RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
     RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]

     RewriteCond %{THE_REQUEST} \s/+sub/(.+?)\.php [NC]
     RewriteRule ^ %1 [R=301,L]

     # remove .php; use THE_REQUEST to prevent infinite loops
     RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
     RewriteRule (.*)\.php$ $1 [L,R=301]

     # remove index
     RewriteRule (.*)/index$ $1/ [R=302]
     # remove slash if not directory
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteCond %{REQUEST_URI} /$
     RewriteRule (.*)/ $1 [R=301,L]

     # add .php to access file, but don't redirect
     RewriteCond %{REQUEST_FILENAME}.php -f
     RewriteCond %{REQUEST_URI} !/$
     RewriteRule (.*) $1.php [L]

     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteRule (?!^sections/)^(.*)$ /sections/$1 [L,NC]

     AuthType Basic
     AuthName "xxx"
     AuthUserFile "/home/xxx/.htpasswds/public_html/passwd"
     require valid-user
完成。htaccess:

 Options +FollowSymLinks -MultiViews -indexes
 RewriteEngine On
 RewriteBase /

 RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
 RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]

 RewriteCond %{THE_REQUEST} \s/+sections/(.+?)(?:\.php)?[/?\s] [NC]
 RewriteRule ^ %1 [R=301,L]

 # remove index
 RewriteCond %{THE_REQUEST} /index(\.php)?[\s?/] [NC]
 RewriteRule ^(.*?)index(/|$) /$1 [L,R=301,NC,NE]

 # remove .php; use THE_REQUEST to prevent infinite loops
 RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
 RewriteRule (.*)\.php$ $1 [L,R=301]

 # remove index
 RewriteRule (.*)/index$ $1/ [R=302]
 # remove slash if not directory
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_URI} /$
 RewriteRule (.*)/ $1 [R=301,L]

 # add .php to access file, but don't redirect
 RewriteCond %{REQUEST_FILENAME}.php -f
 RewriteCond %{REQUEST_URI} !/$
 RewriteRule (.*) $1.php [L]

 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule (?!^sections/)^(.+)$ /sections/$1 [L,NC]

谢谢你anubhava,我试过你的建议用你列出的代码替换它,但是,结果与之前相同。有趣的是,我认为还有其他问题,因为我无法打开主域,例如domain.com,结果出现了一个错误,说明禁止-您没有访问此服务器上的/sections/的权限,但是,如果我手动添加/索引,它会起作用。。有什么想法吗?是的,同样是禁止的,您没有权限访问此服务器上的/sections/。错误确定尝试在.htaccess上添加DirectoryIndex.php index.html行`一旦我删除了我在第一篇文章中发布的最后4行代码,一切都正常,这意味着domain.com可以正常工作,并且所有扩展都被删除,因此/sections/rewrite部分肯定有问题