我是 .htaccess 和正则表达式的新手。我有一个可以在本地运行的脚本,但是当我将其上传到实时服务器时,它会导致无限循环。我该如何解决这个问题?在网站上,http://subdomain.example.com
如果您未经身份验证,则在加载时会带您http://subdomain.example.com/auth/
进入登录表单,否则它会显示 的内容http://subdomain.example.com/index.php
。它在本地运行良好,但是当我在实时服务器上上传时,它找不到 auth.php,因此它会重定向到索引页,并且由于用户未经过身份验证,它会发送到 auth.php,从而导致无限循环。当我从这里测试我的 .htaccess 时http://htaccess.madewithlove.be/结果如下。请指教。请帮忙。谢谢。
这是我的文件
Options +FollowSymLinks -MultiViews -Indexes
<IfModule mod_rewrite.c>
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
# remove php extensions (only for GET method)
RewriteCond %{THE_REQUEST} ^GET\ /(.+)\.php(?:\?|\s) [NC] #This variable is not supported: %{THE_REQUEST}
RewriteCond %{REQUEST_FILENAME} -f #This variable is not supported: %{REQUEST_FILENAME}
RewriteRule ^ %1/? [L,R=301] #This rule was not met because one of the conditions was not met
# don't touch other existing files/folders
RewriteCond %{REQUEST_FILENAME} -f [OR] #This variable is not supported: %{REQUEST_FILENAME}
RewriteCond %{REQUEST_FILENAME} -d [OR] #This variable is not supported: %{REQUEST_FILENAME}
RewriteCond %{ENV:REDIRECT_STATUS} \d #This variable is not supported: %{ENV:REDIRECT_STATUS}
RewriteRule ^ - [L] #This rule was not met because one of the conditions was not met
# force trailing slash
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f #This variable is not supported: %{DOCUMENT_ROOT}/$1\.php
RewriteRule ^(.+?[^/])$ $1/ [L,R=301] #This rule was not met because one of the conditions was not met
RewriteCond %{DOCUMENT_ROOT}/$1.php -f #This variable is not supported: %{DOCUMENT_ROOT}/$1.php
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ $1.php?a=$2&id=$3 [QSA,L]#This rule was not met because one of the conditions was not met
#rewrite path to file eg http://example.com/auth/recover to http://example.com/auth.php?a=recover
RewriteCond %{DOCUMENT_ROOT}/$1.php -f #This variable is not supported: %{DOCUMENT_ROOT}/$1.php
RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?a=$2 [QSA,L] #This rule was not met because one of the conditions was not met
# rewrite extensionless php files
RewriteCond %{DOCUMENT_ROOT}/$1.php -f #This variable is not supported: %{DOCUMENT_ROOT}/$1.php
RewriteRule ^(.+?)/$ $1.php [L] #This rule was not met because one of the conditions was not met
# finally, if not found
RewriteRule . index.php [L] #This rule was met, the new url is http://subdomain.example.com/index.php
</IfModule>
答案1
只需注释掉一些行即可修复此问题,这些行会使服务器附加根目录,从而导致找不到所请求的脚本。工作脚本如下。
Options +FollowSymLinks -MultiViews -Indexes
<IfModule mod_rewrite.c>
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
# remove php extensions (only for GET method)
RewriteCond %{THE_REQUEST} ^GET\ /(.+)\.php(?:\?|\s) [NC]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ %1/? [L,R=301]
# don't touch other existing files/folders
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{ENV:REDIRECT_STATUS} \d
RewriteRule ^ - [L]
# force trailing slash
#RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+?[^/])$ $1/ [L,R=301]
#RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ $1.php?a=$2&id=$3&att=$4 [QSA,L]
#RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ $1.php?a=$2&id=$3 [QSA,L]
#rewrite path to file eg http://example.com/auth/recover to http://example.com/auth.php?a=recover
#RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?a=$2 [QSA,L]
# rewrite extensionless php files
#RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/$ $1.php [L]
# finally, if not found
RewriteRule . index.php [L]
</IfModule>