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]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 gki vendor hook
  • ¥15 centos7中sudo命令无法使用
  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 这是一个利用ESP32自带按键和LED控制的录像代码,编译过程出现问题,请解决并且指出错误,指导如何处理 ,协助完成代码并上传代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。
  • ¥20 求结果和代码,sas利用OPTEX程序和D-efficiency生成正交集
  • ¥50 adb连接不到手机是怎么回事?
  • ¥20 抓取数据时发生错误: get_mooncake_data() missing 1 required positional argument: 'driver'的问题,怎么改出正确的爬虫代码?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联