检查网站是否已由.htaccess重定向


Check whether site was redirected in by .htaccess

我在.htaccess文件中有以下规则:

RewriteRule orders'.html orders.php [L,NC]

如何在orders.php中检查用户输入的是orders.html还是orders.php作为请求URL?

如果用户在地址栏中有orders.php,我想将其重定向到orders.html。

如果用户在地址栏中有orders.php,我想将其重定向到orders.html

如果你正在寻找.htaccess解决方案,请尝试下面的代码

RewriteEngine on
RewriteBase /
#if the user requested orders.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}' /orders'.php [NC]
#301 redirect them to orders.html
RewriteRule . orders.html [L,R=301]
#existing rule to rewrite .html to .php
RewriteRule ^orders'.html$ orders.php [L,NC]

为了在重写后从PHP获得最初请求的URI:

  1. 尝试$_SERVER['REDIRECT_URL']。根据我的经验,Apache的mod_rewrite模块为CGI填充REDIRECT_URL变量。

  2. $_SERVER['REQUEST_URI']也应该是有用的。

执行print_r($_SERVER);以查看您可以使用的所有变量。