如何修复加密本地脚本调用时的HTTP重定向错误?(.htaccess)
解决本地链接未触发HTTPS重定向并指向指定路径的问题
我的硬编码脚本在点击提交按钮时会调用链接:http://127.0.0.1/?act=Arcade&do=newscore。我已强制启用HTTPS,但该链接调用时未触发重定向,需要将其重定向到index.php?act=Arcade&do=newscore。
当前.htaccess配置如下:
Options All -Indexes # Swap index.html index.php for the HTML start page DirectoryIndex index.php index.html Options +FollowSymlinks <IfModule mod_rewrite.c> RewriteEngine on # Force SSL - no insecure data connections! RewriteCond %{HTTPS} off RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$ RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$ RewriteRule (.*) https://%{HTTP_HOST}:443%{REQUEST_URI} # Force WWW - this is only needed for root domains... RewriteCond %{HTTP_HOST} !="" RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteCond %{HTTPS}s ^on(s)| RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] # Security Rewrites RewriteCond %{THE_REQUEST} (\?act\=) [NC,OR] RewriteCond %{THE_REQUEST} (sql_login) [NC,OR] RewriteCond %{THE_REQUEST} (basepath) [OR] RewriteCond %{THE_REQUEST} (libpath) [OR] RewriteCond %{THE_REQUEST} (absolute_path) [OR] RewriteCond %{THE_REQUEST} (vwar_root) [OR] RewriteCond %{THE_REQUEST} (includedir) [OR] RewriteCond %{THE_REQUEST} (file=http:\/\/) [OR] RewriteCond %{THE_REQUEST} (name=http:\/\/) [OR] RewriteCond %{THE_REQUEST} (phpbb_root_path) RewriteRule ^.*$ http://127.0.0.1/ [R=301,L] </IfModule>
解决方案
需要做两处修改:
- 添加目标链接的重定向规则:在「Force WWW」规则之后、「Security Rewrites」规则之前,插入以下代码,专门处理目标链接的重定向:
# 重定向127.0.0.1的Arcade提交请求到index.php RewriteCond %{HTTP_HOST} ^127\.0\.0\.1$ [NC] RewriteCond %{QUERY_STRING} ^act=Arcade&do=newscore$ [NC] RewriteRule ^$ https://%{HTTP_HOST}/index.php?%{QUERY_STRING} [R=301,L]
- 修改安全规则排除合法请求:原安全规则会拦截所有带
?act=的请求,包括我们需要的合法链接。修改第一个安全条件,排除目标请求:
将
RewriteCond %{THE_REQUEST} (\?act\=) [NC,OR]
替换为
RewriteCond %{THE_REQUEST} (\?act\=)(?!Arcade&do=newscore) [NC,OR]
修改后的完整.htaccess配置
Options All -Indexes # Swap index.html index.php for the HTML start page DirectoryIndex index.php index.html Options +FollowSymlinks <IfModule mod_rewrite.c> RewriteEngine on # Force SSL - no insecure data connections! RewriteCond %{HTTPS} off RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$ RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$ RewriteRule (.*) https://%{HTTP_HOST}:443%{REQUEST_URI} # Force WWW - this is only needed for root domains... RewriteCond %{HTTP_HOST} !="" RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteCond %{HTTPS}s ^on(s)| RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] # 重定向127.0.0.1的Arcade提交请求到index.php RewriteCond %{HTTP_HOST} ^127\.0\.0\.1$ [NC] RewriteCond %{QUERY_STRING} ^act=Arcade&do=newscore$ [NC] RewriteRule ^$ https://%{HTTP_HOST}/index.php?%{QUERY_STRING} [R=301,L] # Security Rewrites RewriteCond %{THE_REQUEST} (\?act\=)(?!Arcade&do=newscore) [NC,OR] RewriteCond %{THE_REQUEST} (sql_login) [NC,OR] RewriteCond %{THE_REQUEST} (basepath) [OR] RewriteCond %{THE_REQUEST} (libpath) [OR] RewriteCond %{THE_REQUEST} (absolute_path) [OR] RewriteCond %{THE_REQUEST} (vwar_root) [OR] RewriteCond %{THE_REQUEST} (includedir) [OR] RewriteCond %{THE_REQUEST} (file=http:\/\/) [OR] RewriteCond %{THE_REQUEST} (name=http:\/\/) [OR] RewriteCond %{THE_REQUEST} (phpbb_root_path) RewriteRule ^.*$ http://127.0.0.1/ [R=301,L] </IfModule>
内容的提问来源于stack exchange,提问作者Ernest Allen Buffington
相关产品推荐
相关产品推荐

