6

我已经添加了这个 .htaccess 来从 URL 中删除文件扩展名,所以它会一直只显示“index.php”而不是“index.php”。但是在我这样做之后,我<form method="post">停止了工作

Options +FollowSymLinks -MultiViews
Options +Indexes
AcceptPathInfo Off
RewriteEngine on
RewriteBase   /

#Force from http to https
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{HTTP_HOST} !^mysite.com$
RewriteRule ^(.*)$ https://mysite.com/$1 [R=301]

#take off index.html
 RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.html$ [NC]
RewriteRule . http://www.%{HTTP_HOST}%1 [R=301,NE,L]

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

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]    

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

## To internally redirect /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [L]  

这是一个例子:

/* Worked before .htaccess, but not anymore */

    <form method="post" action="pwreset.php"  class="form-stacked">

/* Removing .php from action works. But there are hundreds of files and this method is not very trustworthy */

    <form method="post" action="pwreset"  class="form-stacked">

PS:如果我使用常规的 .htaccess 规则,比如这个:

重写规则 ^index$ ./index.php [L,NC]

它不会在所有情况下隐藏 .php

4

2 回答 2

7

发生这种情况是因为您在此处重定向:

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

当您重定向时,请求 BODY 并不总是包含在内,您可以尝试为 POST 添加异常:

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{REQUEST_METHOD} !POST [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
于 2013-11-05T19:04:53.533 回答
5

因为action您的表单指向/pwreset.php. 当您尝试访问该页面(甚至通过表单发布)时,htaccess 将/pwreset在任何 PHP 代码运行之前将您重定向到该页面。重定向将删除POST新请求中的所有数据。

您将不得不更改所有这些表单操作以获得非 php 版本。作为短期修复,请尝试从重定向规则中排除 POST 请求

RewriteCond %{REQUEST_METHOD} !POST

于 2013-11-05T19:06:59.057 回答