有没有办法处理 ajax 请求不同于 .htaccess 中的用户请求?
Is there a way to treat ajax requests different than user requests in .htaccess?
假设我有 /secret/example.php
在我的 .htaccess 文件中,我想使用
将对 /secret
的请求重写为 /home.html
RewriteRule secret /home.html
但现在当我尝试使用 example.js
中的 ajax 访问 /secret/example.php
时
var xhr = new XMLHttpRequest();
xhr.open('GET', '/secret/example.php', true);
xhr.onload = function(){
console.log(xhr.responseText);
}
xhr.send();
它也被重定向了,但不起作用。
有没有一种方法可以只重写用户发出的请求(通过在搜索栏中键入 www.example.com/secret
)并仍然允许 ajax 访问该文件?
如果在 xhr 请求中设置额外的 header 是可能的:xhr.setRequestHeader("VIAXHR", "true");
然后在 .htaccess 中是这样的:
RewriteCond %{HTTP:VIAXHR} !^true$
RewriteRule secret /home.html
在 RewriteRule 之前使用 RewriteCond
像这样:
RewriteCond %{REQUEST_URI} !^/secret/example.php$
RewriteRule secret /home.html
假设我有 /secret/example.php
在我的 .htaccess 文件中,我想使用
/secret
的请求重写为 /home.html
RewriteRule secret /home.html
但现在当我尝试使用 example.js
/secret/example.php
时
var xhr = new XMLHttpRequest();
xhr.open('GET', '/secret/example.php', true);
xhr.onload = function(){
console.log(xhr.responseText);
}
xhr.send();
它也被重定向了,但不起作用。
有没有一种方法可以只重写用户发出的请求(通过在搜索栏中键入 www.example.com/secret
)并仍然允许 ajax 访问该文件?
如果在 xhr 请求中设置额外的 header 是可能的:xhr.setRequestHeader("VIAXHR", "true");
然后在 .htaccess 中是这样的:
RewriteCond %{HTTP:VIAXHR} !^true$
RewriteRule secret /home.html
在 RewriteRule 之前使用 RewriteCond
像这样:
RewriteCond %{REQUEST_URI} !^/secret/example.php$
RewriteRule secret /home.html