douhui3330
2014-10-23 18:54
浏览 70
已采纳

.htaccess重定向循环错误

I created a .htaccess with the following goals:

1: Remove .php and force add trailing slash

2: Make http://website.com/sticker/fruit/apple = sticker.php?category=fruit&alt=apple

That works, but whenever I type something that does not exist, for example:

http://website.com/sdgsdg/ OR http://website.com/dssdg/sdgsg/zkzzv/

I get redirect errors.

Here is my .htacess:

Options +FollowSymlinks
RewriteEngine on

# Remove .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php/$ http://website.com/$1/ [L,R=301]

# Remove .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ http://website.com/$1/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ http://website.com/$1/ [L,R=301]

## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]
RewriteRule ^sticker/([^/.]+)/([^/.]+)/$ sticker.php?category=$1&alt=$2 [NC,L]

Can anyone spot whats causing the redirect loops? Thank you in advance.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duane2364 2014-10-23 21:59
    已采纳

    I found a solution that worked for me.

    Options +FollowSymlinks
    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} ^/(.+)/$
    RewriteCond %{DOCUMENT_ROOT}/%1.php -f
    RewriteRule ^(.*)/$ $1.php [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} ^/(.+)/$
    RewriteCond %{DOCUMENT_ROOT}/%1.php -f
    RewriteRule ^(.*)$ $1.php [L]
    
    RewriteCond %{REQUEST_URI} /+[^\.]+$
    RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -l
    RewriteRule ^ - [L]
    RewriteRule ^sticker/([^/.]+)/([^/.]+)/$ sticker.php?category=$1&alt=$2 [NC,L]
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题