mod_rewrite重定向循环/打破我的网站

时间:2015-05-23 08:49:58

标签: php apache .htaccess mod-rewrite redirect

我想要的是什么:

  • /foo.php在网址栏中显示/foo
  • /foo重定向到/foo.php
  • 两者都必须查看/foo.php
  • 必须使用/showthread.php?id=00/showthread?id=00)等

即使我到处都使用[L],我也会不断获得重定向循环。 我试过这些:

(1)
RewriteRule ^/forums$ forums.php [R]
RewriteRule ^/forums\.php$ forums [PT]
(2)
RewriteRule ^forums\.php/?$ forums [L,R=301]
RewriteRule ^forums/?$ forums.php
(3)
RewriteRule ^forums/?$ forums.php [L,R=301]
(4)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([a-z/]+).php$ $1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]

以上每项工作都针对个别foo.phpfoofoofoo.php,但两者都有无限循环。

我也试过这个:

## don't touch /forum URIs
# RewriteRule ^forums/ - [L,NC]

## hide .php extension snippet

# To externally redirect /dir/foo.php to /dir/foo
# RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
# RewriteRule ^ %1 [QSA,R,L]

# To internally forward /dir/foo to /dir/foo.php
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_FILENAME}.php -f
# RewriteRule ^(.*?)/?$ $1.php [QSA,L]

哪个工作正常,但由于某种原因打破了很多网站功能。 (搜索栏不起作用,论坛不起作用,登录不起作用)

我需要foo.php才能继续工作,因为MyBB使用它。

1 个答案:

答案 0 :(得分:1)

在root .htaccess中尝试此代码:

RewriteEngine On

RewriteRule ^forums/ - [L,NC]

## hide .php extension snippet

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]