.htaccess causes infinite loop on live server but

2019-09-08 15:16发布

I'm a newbie .htaccess and regex. I have a script that works locally but when I upload it to a live server it causes an infinite loop. How can I fix this? On the site when you load http://example.com if you are unauthenticated it takes you to http://example.com/auth/ for a login form, else it displays the contents of http:///example.com/index.php. It works fine locally but when I upload on a live server, it cannot find auth.php so it redirects to the index page and because the user is not authenticated it sends to auth.php thus the infinite loop. Please help. Thanks.

This is my file

Options -Indexes
Options -MultiViews    
Options +FollowSymLinks

<IfModule mod_rewrite.c>
   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
   RewriteRule ^ - [L]

   # force trailing slash
   RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
   RewriteRule ^(.+)$ $1/ [L,R=301] 

   # rewrite extensionless php files
   RewriteCond %{DOCUMENT_ROOT}/$1.php -f
   RewriteRule ^(.+)/$ $1.php [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]

   RewriteCond %{DOCUMENT_ROOT}/$1.php -f
   RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ $1.php?a=$2&id=$3 [QSA,L]

   # finally, if not found
   RewriteRule ^ index.php [L]
</IfModule>

2条回答
Melony?
2楼-- · 2019-09-08 15:38

You can reorder your rules:

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 [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>
查看更多
Explosion°爆炸
3楼-- · 2019-09-08 15:40

After googling and reading and trying and testing, I got the way to resolve godaddy's funny .htaccess probles. I solved it here.

https://serverfault.com/questions/720085/htaccess-causes-infinite-loop-on-live-server-but-works-on-localhost/

查看更多
登录 后发表回答