添加尾部斜杠和重定向301后的htaccess无限循环

时间:2014-05-07 18:06:07

标签: apache .htaccess redirect

我的htaccess有问题。我想在我的URL中添加一个尾部斜杠。我目前正在本地开发。所以这是htaccess:

Options +FollowSymlinks
RewriteEngine On

#RewriteBase 
RewriteBase /balade-gourmande

DirectoryIndex index.php

# Add ending slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ %{REQUEST_URI}/ [R=301,L] 


# Main pages
RewriteRule ^(.*)/(.*)/$                $2.php [L]
RewriteRule ^(.*)/$                 $1.php [L]

正如您所看到的,我在localhost上有很多其他网站(为什么我有重写库)。我所有的.php文件都在“balade-gourmande”目录中。 我在balade-gourmande文件夹中有一个index.php和一个yes.php文件。

如果我尝试:

localhost / balade-gourmande / - >没关系,我登陆index.php

localhost / balade-gourmande / yes - >没关系,我登陆yes.php

localhost / balade-gourmande / yes / - >没关系,我登陆yes.php

问题是如果我输错了,ex: 本地主机/ balade-美食店/什么 它给了我一个:localhost / balade-gourmande / whatever.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php / 无限循环错误...

有人可以帮助我,如果你需要更多信息,我会尽力而为。

THX!

**编辑: 这是位于根目录中的htaccess,而不是我的“balade-gourmande /”:

Options +FollowSymlinks
RewriteEngine On

#RewriteBase 

DirectoryIndex index.php

# Error
ErrorDocument 404 '/errorhandler.php'

# Add ending slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ %{REQUEST_URI}/ [R=301,L] 


# Page principales
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

2 个答案:

答案 0 :(得分:0)

htaccess的

# ----------------------------------------------------------------------
# Start rewrite engine
# ----------------------------------------------------------------------

# Turning on the rewrite engine is necessary for the following rules and features.
# FollowSymLinks must be enabled for this to work.

<IfModule mod_rewrite.c>
 # Make sure directory listing is disabled
Options +FollowSymLinks -Indexes
RewriteEngine on

# Send request via index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d


RewriteRule ^(.*)$ index.php/$1 [L]



</IfModule>

希望它对你有所帮助,我从未犯过错误

答案 1 :(得分:0)

# Main pages部分中的2条规则替换为以下规则:

DirectoryIndex index.php
ErrorDocument 404 /balade-gourmande/errorhandler.php
Options +FollowSymlinks -MultiViews
RewriteEngine On

# Add ending slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s/+.*?[^/][\s?]
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

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