簡體   English   中英

如何將%3f重寫為? 在阿帕奇

[英]How to re-write %3f to ? in apache

我們的大多數頁面都是通過查詢字符串訪問的,因此我們的URL如下所示:

http://www.example.com/?var=val&var2=val2

我發現在網絡上的某個地方,有人通過以下鏈接鏈接回我們:

http://www.example.com/%3Fvar%3Dval%26var2%3Dval2

我發現了一大段代碼要添加到我的.htaccess文件中,但這確實減慢了我的頁面請求速度。 我想要的是在文件名的開頭捕獲“%”並將其重定向到php文件,在其中我可以將其解析為查詢字符串並進行301重定向。

我有一種感覺,如果我知道自己在做什么,那實際上是一件很容易的事情。 讓我們假設我的php文件將被稱為percent_fix.php

(我確定我可以編寫php,我只需要.htaccess重寫條件和規則方面的幫助。)

嘗試為您的鏈接提供默認頁面。 例如index.php

然后添加到您的.htaccess

# for %3f ...
RewriteRule ^/index.php\?(.*)$ /index.php?$1

您幾乎肯定會收到403錯誤。 該錯誤是由於? 是Windows和Linux上被禁止的文件/目錄名稱字符。 這意味着當Apache嘗試查找名為“ /document/root/index.php?blah”的文件或目錄時(解碼后),這會導致403錯誤。 這是在讀取.htaccess文件之前,因此您無法在.htaccess文件中使用mod_rewrite覆蓋此403錯誤,也不可以在.htaccess文件中使用ErrorDocument捕獲此錯誤。

捕獲%3f的唯一方法是在“ VirtualHost”中使用mod_rewrite或ErrorDocument,例如在httpd-vhosts.conf中(或在主服務器配置中,如果在httpd.conf中沒有任何“虛擬主機”)。

在您的.htaccess文件中嘗試以下操作:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php$1 [L,NC]

我回到問題中包含的鏈接,並意識到該線程頂部附近的簡單情況確實可以滿足我的需求。 這是對我真正有效的方法:

#in .htaccess
RewriteEngine on

# If an encoded "?" is present in the requested URI, and no unencoded "?" is
# present, then externally redirect to replace the encoded "?" character.
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^?\ ]+)\ HTTP/
RewriteCond %1 ^(([^%]*(\%(25)*([^3].|.[^F]))*)*)\%(25)*3F(.*)$ [NC]
RewriteRule ^. http://www.example.com/percent_fix.php?%7 [NE,R=301,L]

然后在percent_fix.php中

<?php
if($_SERVER['QUERY_STRING'])
  {
    $new_query=urldecode($_SERVER['QUERY_STRING']);
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: http://www.example.com/?'.$new_query);
    die();
  }
else
  {
    header('HTTP/1.x 404 Not Found');
    //readfile("http://www.example.com/?page=404_error");
    die();
  }
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM