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

发布于 2024-07-30 06:32:29 字数 637 浏览 7 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半岛未凉 2024-08-06 06:32:29

试试这个:

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

不是,您只需将 443 替换为您的 SERVER_HTTPS_PORT 值,将 www.example.com 替换为您的 CLIENT_DOMAIN > 价值。

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.

情绪失控 2024-08-06 06:32:29

这应该适合您(将 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]

This should work for you (replace www.google.com with your 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]
み青杉依旧 2024-08-06 06:32:29

看看Apache的mod_rewrite的文档,可能有一个方式,在 RewriteCond 条件中使用 %{REQUEST_METHOD} ; 像这样的东西可能会起作用:

RewriteCond %{REQUEST_METHOD} !=POST

当然,接下来是所需的 RewriteRule 将所有内容重定向到非 POST 页面。

我现在无法进行测试,所以这可能并不完美,但类似的东西也许可以解决问题——或者至少引导您找到解决方案:

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

的想法

  • 匹配所有内容
  • 到匹配的内容
  • 重定向 查询字符串(QSA 标志)
  • 并使用 301 代码进行重定向
  • 并在那里停止

Lookink at the documentation of Apache's mod_rewrite, there might be a way, using %{REQUEST_METHOD} in a RewriteCond condition ; 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.

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)
  • and redirecting with a 301 code
  • and stopping there
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文