【问题标题】:stop .htaccess from redirecting subfolder/index.php to root阻止 .htaccess 将子文件夹/index.php 重定向到根目录
【发布时间】:2014-08-20 16:20:35
【问题描述】:

我有一个似乎无法调试的问题。我尝试了很多解决方案,但都没有运气。

当我使用 subfolder/index.php 访问 URL 时,我会被重定向回根页面。如果我访问该子文件夹中的另一个页面,它工作正常。我不确定是什么导致我的代码出现这种情况。这是我所拥有的。

Options +FollowSymLinks -MultiViews 
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule (.*) http://www.domain.com/$1 [R=301,L] 

RewriteCond %{HTTP_HOST} ^www.domain\.com
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301] 

# remove index
RewriteRule (.*)index$ $1 [R=301]

# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]

# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

有什么建议吗?谢谢!

【问题讨论】:

    标签: php apache .htaccess redirect subdirectory


    【解决方案1】:

    保持这样的规则:

    Options +FollowSymLinks -MultiViews 
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} ^domain\.com$
    RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L,NE] 
    
    RewriteCond %{HTTP_HOST} ^www.domain\.com
    RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
    RewriteRule ^ /%1 [R=301,L,NE]
    
    # remove slash if not directory
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} /$
    RewriteRule ^(.+?)/$ $1 [R=301,L,NE]
    
    # add .php to access file, but don't redirect
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule (.*) $1.php [L]
    

    【讨论】:

      猜你喜欢
      • 2012-10-23
      • 2017-01-29
      • 2021-09-03
      • 2011-01-19
      • 2012-04-27
      • 2011-01-14
      • 1970-01-01
      • 2014-05-15
      相关资源
      最近更新 更多