php 到 htaccess 重写 - 如果没有 $_POST 数据则重定向
if( count( $_POST ) < 1 ) {
// determine if this was a secure request - we use a non standard HTTPS port so the SERVER_HTTPS_PORT define should always be used in place of 443
$protocol = $_SERVER['SERVER_PORT'] == SERVER_HTTPS_PORT ? 'https' : 'http';
header( "HTTP/1.0 301 Moved Permanently" );
header( "Status: 301" ); // this is for chrome compliance
header( "Location: $protocol://".CLIENT_DOMAIN."{$_SERVER['REQUEST_URI']}" );
session_write_close();
exit;
}
这个功能可以用 .htaccess 规则重写吗?
逻辑:
如果不是 POST 请求,则通过发出 301 标头和状态来重定向到包含整个查询字符串的等效页面,同时维护协议。
if( count( $_POST ) < 1 ) {
// determine if this was a secure request - we use a non standard HTTPS port so the SERVER_HTTPS_PORT define should always be used in place of 443
$protocol = $_SERVER['SERVER_PORT'] == SERVER_HTTPS_PORT ? 'https' : 'http';
header( "HTTP/1.0 301 Moved Permanently" );
header( "Status: 301" ); // this is for chrome compliance
header( "Location: $protocol://".CLIENT_DOMAIN."{$_SERVER['REQUEST_URI']}" );
session_write_close();
exit;
}
Can this functionality be rewritten with .htaccess rules?
Logic:
If not a POST request, redirect to equivalent page with whole query string by issuing 301 header and status, whilst maintaining protocol.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
不是,您只需将
443
替换为您的SERVER_HTTPS_PORT
值,将www.example.com
替换为您的CLIENT_DOMAIN
> 价值。Try this:
Not you just need to replace
443
by yourSERVER_HTTPS_PORT
value andwww.example.com
by yourCLIENT_DOMAIN
value.这应该适合您(将
www.google.com
替换为您的CLIENT_DOMAIN
)。This should work for you (replace
www.google.com
with yourCLIENT_DOMAIN
).看看Apache的mod_rewrite的文档,可能有一个方式,在
RewriteCond
条件中使用%{REQUEST_METHOD}
; 像这样的东西可能会起作用:当然,接下来是所需的
RewriteRule
将所有内容重定向到非 POST 页面。我现在无法进行测试,所以这可能并不完美,但类似的东西也许可以解决问题——或者至少引导您找到解决方案:
的想法
Lookink at the documentation of Apache's mod_rewrite, there might be a way, using
%{REQUEST_METHOD}
in aRewriteCond
condition ; something like this might do the trick :Followed, of course, by the needed
RewriteRule
to redirect everything to the non-POST page.I have no way of testing right now, so this might not be perfect, but something like this could maybe do the trick -- or, at least, guide you to the solution :
The idea beeing to