旧的.htaccess:

旧的.htaccess:

我想要的是:

  • 力量 www[作品]
  • 限制对 .inc.php 的访问[作品]
  • 强制将 abc.php 重定向到 /abc/
  • 从 URL 中删除扩展名
  • 如果需要,添加尾部斜杠

旧的.htaccess:

    Options +FollowSymLinks


    <IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteBase /

    ### Force www
    RewriteCond %{HTTP_HOST} ^example\.net$
    RewriteRule ^(.*)$ http://www\.example\.net/$1 [L,R=301]


    ### Restrict access
    RewriteCond %{REQUEST_URI} ^/(.*)\.inc\.php$ [NC]
    RewriteRule .* - [F,L]


    #### Remove extension:
    RewriteRule ^(.*)/$ /$1.php [L,R=301]



    ######### Trailing slash:
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ http://www.example.net/$1/ [R=301,L]
    </IfModule>

新的 .htaccess:

     Options +FollowSymLinks


     <IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteBase /

    ### Force www
    RewriteCond %{HTTP_HOST} ^example\.net$
    RewriteRule ^(.*)$ http://www\.example\.net/$1 [L,R=301]


    ### Restrict access
    RewriteCond %{REQUEST_URI} ^/(.*)\.inc\.php$ [NC]
    RewriteRule .* - [F,L]


    #### Remove extension:
    RewriteCond %{REQUEST_FILENAME} \.php$
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule (.*)\.php$ /$1/ [L,R=301]

    #### Map pseudo-directory to PHP file
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule (.*) /$1.php [L]

    ######### Trailing slash:
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteCond %{REQUEST_FILENAME} !/$
    RewriteRule (.*) $1/ [L,R=301]

    </IfModule>

错误日志:

     Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: http://www.example.net/

重写.日志:http://pastebin.com/x5PKeJHB

答案1

这似乎是您最初的问题:

#### Map pseudo-directory to PHP file
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule (.*) /$1.php [L]

您捕获了一个‘/’字符。

119.154.34.231 - - [03/Oct/2010:16:17:06 +0000] [www.example.net/sid#7f3fa2d89670][rid#7f3fa3466b58/initial] (4) [perdir /var/www/example.net/public_html/] RewriteCond: input='/var/www/example.net/public_html/how-works.php' pattern='-f' => 已匹配

119.154.34.231 - - [03/Oct/2010:16:17:06 +0000] [www.example.net/sid#7f3fa2d89670][rid#7f3fa3466b58/initial] (2) [perdir /var/www/example.net/public_html/] 重写 'how-works/' -> '/how-works/.php'

尝试:

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

...我可能有点错,但这似乎是你首先出错的地方。

答案2

您正在下一个块中捕获在“删除扩展”块中添加的尾部斜杠,这会中断对本地文件的调用,并且将继续附加在 .php 上,因为“尾部斜杠”块永远不会匹配,所以它会绕回到“Map psudo-dir”块。

添加'(/?)'反向引用,你的$1中是否不小心有“/”####将伪目录映射到PHP文件RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule /(.*)(/?) /$1.php [L]

并且,据我所知,“-d”应该是“-f”:

### 尾部斜杠:
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !/$
RewriteRule (.*) $1/ [L,R=301]

虽然这看起来有点复杂。你应该让你的站点链接到“/abc/”目录,而不是直接链接到 php 文件,然后重定向它们。

相关内容