简体   繁体   English

php到htaccess重写-如果没有$ _POST数据则重定向

[英]php to htaccess rewrite - redirect if no $_POST data

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? 可以使用.htaccess规则重写此功能吗?

Logic: 逻辑:

If not a POST request, redirect to equivalent page with whole query string by issuing 301 header and status, whilst maintaining protocol. 如果不是POST请求,则通过发出301标头和状态,同时保持协议,将整个查询字符串重定向到等效页面。

Try this: 尝试这个:

RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{SERVER_PORT}s ^443(s)|.*
RewriteRule ^foo/bar$ http%1://www.example.com%{REQUEST_URI} [L,R=301]

Not you just need to replace 443 by your SERVER_HTTPS_PORT value and www.example.com by your CLIENT_DOMAIN value. 并非只需要用SERVER_HTTPS_PORT值替换443 ,用CLIENT_DOMAIN值替换www.example.com

This should work for you (replace www.google.com with your CLIENT_DOMAIN ). 这应该适合您(将www.google.com替换为CLIENT_DOMAIN )。

RewriteEngine on
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{REQUEST_METHOD} !^POST$ [NC]
RewriteRule ^(.*)$ http://www.google.com/$1 [L,QSA,R=301]
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{REQUEST_METHOD} !^POST$ [NC]
RewriteRule ^(.*)$ https://www.google.com/$1 [L,QSA,R=301]

Lookink at the documentation of Apache's mod_rewrite , there might be a way, using %{REQUEST_METHOD} in a RewriteCond condition ; 看看Apache的mod_rewrite的文档,可能有一种在RewriteCond条件下使用%{REQUEST_METHOD}的方法; something like this might do the trick : 这样的事情可能会解决问题:

RewriteCond %{REQUEST_METHOD} !=POST

Followed, of course, by the needed RewriteRule to redirect everything to the non-POST page. 当然, RewriteRule是所需的RewriteRule ,可将所有内容重定向到非POST页面。

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 : 我目前无法进行测试,因此这可能并不完美,但是类似的事情可能可以解决问题-或至少可以指导您找到解决方案:

RewriteRule ^(.*)$ $1 [QSA,R=301,L]

The idea beeing to 的想法

  • match everything 匹配一切
  • redirect to what was matched 重定向到匹配的内容
  • keeping the Query String (QSA flag) 保留查询字符串(QSA标志)
  • and redirecting with a 301 code 并使用301代码重定向
  • and stopping there 然后停在那里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM