.htaccess拒绝.php扩展但无法触发AJAX请求

时间:2016-10-01 16:30:03

标签: php .htaccess mod-rewrite

我添加了.htaccess tweak来拒绝对.php文件的所有请求,并在内部将所有非.php请求重定向到.php文件。

在我的LocalHost上一切正常,但在生产环境中文件上传脚本失败(我发现文件上传脚本使用JS / Ajax调用php文件,这是不加载的)

我的.htaccess配置是:

RewriteEngine on

# Rewrite /foo/bar to /foo/bar.php
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]

# Return 404 if original request is /foo/bar.php
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]

并且JS调用是:

<form class="dropzone" id="dropzoneForm" action="ads_upload"
                        enctype="multipart/form-data" method="post">
                        <div class="fallback">
                            <input name="file" type="file" multiple />
                        </div>
                    </form>

...............

 $.post("ads_upldDelete?fname=" + file.name);//up-deletefile.php

ads_upload.php和ads_upldDelete.php都在服务器中存在,但由于模式重写规则无法到达,我可以在模式重写中写入和异常以允许吗?

1 个答案:

答案 0 :(得分:0)

您可以使用:

RewriteEngine on

# Rewrite /foo/bar to /foo/bar.php
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]

# Return 404 if original request is /foo/bar.php
RewriteCond %{REQUEST_URI} !ads_up(?:load|ldDelete)\.php$ [NC]
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]

并使用ads_upload.phpads_upldDelete.php链接。