2017-09-11 86 views
3

我有一個託管在共享Microsoft-IIS/8.5服務器上的網頁。 我沒有該服務器的管理員權限,而且我也無法訪問其配置文件,因此我無法確定URL rewrite模塊或HTTP redirect元素是否已正確安裝/設置。在共享主機上忽略web.config重定向

我想建立一個從名爲old_folder的文件夾到名爲new_folder的文件夾(都位於我的根目錄,即http://sharedhosting.com/myusername/)。 我放在根目錄下面的文件web.config

<?xml version="1.0" encoding="UTF-8"?> 

<configuration> 
    <configSections> 
     <sectionGroup name="system.webServer"> 
      <sectionGroup name="rewrite"> 
       <section name="rules" overrideModeDefault="Allow" /> 
      </sectionGroup> 
     </sectionGroup> 
    </configSections> 

    <system.webServer> 
     <rewrite> 
      <rules> 
       <rule name="attempt" stopProcessing="true" enabled="true"> 
       <match url="^old_folder/$" /> 
       <action type="Redirect" url="new_folder/" redirectType="Permanent"/> 
       </rule> 
      </rules> 
     </rewrite> 
    </system.webServer> 
</configuration> 

但所有我能得到的是一個403 - Forbidden: Access is denied.(如果我離開的空old_folder)或404 - File or directory not found.(如果我刪除它)。

我嘗試使用HTTP Redirect,在old_folder放置Web.config文件,其中包含

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <system.webServer> 
     <httpRedirect enabled="true" destination="http://sharedhosting.com/myusername/new_folder/" /> 
    </system.webServer> 
</configuration> 

但是這並沒有任何工作。

我知道我的Web.config文件被讀取(如果它們中有錯誤,我可以得到500個錯誤)。我懷疑已經安裝了URL重寫,因爲如果存在folder文件夾,則例如http://sharedhosting.com/myusername/folder被重寫爲http://sharedhosting.com/myusername/folder/(即,以斜槓)。

我的規則是否正確?我的主機是否阻止我重定向?如果是的話,我怎麼知道?

我在<rules>標記後添加了一個<clear />以放棄所有其他重寫規則,但它仍然沒有重定向任何內容。

我錯過了什麼?

最後一次編輯BOUNTY到期之前 爲了澄清,我的問題是「如何理解爲什麼服務器無法正確解釋/無視我的指示?」。

We came to the conclusion這可能是我的服務器奇怪的設置,我正在試圖「恢復工程」,並檢測是什麼讓服務器忽略重定向/重寫規則。

回答

3

你可以通過使用HttpHandlers來達到同樣的效果,所以我假設你可以在瀏覽器中直接訪問Old_folderNew_folder

下面是一個簡單httphandlers,你可以這樣做:

youname.Handlers 
{ 
    public class RedirectHandler : IHttpHandler 
    { 
     public void ProcessRequest(HttpContext context) 
    { 
     context.Response.Status = "301 Moved Permanently"; 
     string url = context.Request.Url.ToString().Replace("old_folder", "new_folder"); 
     context.Response.AddHeader("Location", url); 
     } 
     public bool IsReusable 
    { 
     get { return false; } 
    } 
    } 
} 

web_config添加處理程序如下:

system.web> 
    <httpHandlers> 
    <add verb="*" path="xxx/*.*" type="yourname.Handlers.RedirectHandler"/> 
    </httpHandlers> 
</system.web> 

使用IIS Url_rewriting:

<?xml version="1.0"?> 
<configuration> 
    <system.webServer> 
     <rewrite> 
      <rewriteMaps> 
       <rewriteMap name="yourRedirects"> 
        <add key="/yourroot/old_folder/" value="/otherdir/new_folder" /> 
       </rewriteMap> 
      </rewriteMaps> 
      <rules> 
       <rule name="RedirectRule" stopProcessing="true"> 
        <match url=".*" /> 
        <conditions> 
         <add input="{yourRedirects:{REQUEST_URI}}" pattern="(.+)" /> 
        </conditions> 
        <action type="Redirect" url="http://www.yourdomain.com{C:1}" appendQueryString="False" redirectType="Permanent" /> 
       </rule> 
      </rules> 
     </rewrite> 
    </system.webServer> 
</configuration> 
+0

@Clément更新你的問題與你如何嘗試...其難以閱讀的評論 – Webruster

+0

@Clément我提供了2個解決方案,其中一個用'IIS url'重寫另一個用'http handlers' – Webruster

+0

對不起但我完全不知道如何使用'httphandlers'。我不熟悉'asp.net',所以我試圖測試,但我甚至不能告訴你涉及'httphandlers'的解決方案是否工作。正如我所說的,涉及'web.config'的解決方案不起作用,我非常樂觀。非常感謝您的幫助,我很感激。 –

3

我認爲這可以做無線一個簡單的重寫規則。

<rule name="ToNewFolder" stopProcessing="true"> 
    <match url="^old_folder/(.*)" ignoreCase="true" /> 
    <action type="Rewrite" url="new_folder/{R:1}" /> 
</rule> 

上述規則將所有請求發送到新文件夾,但保留舊網址old_folder在其中。如果你想要的網址,以改變它的顯示器以它new_folder,使用

<rule name="ToNewFolder" stopProcessing="true"> 
    <match url="^old_folder/(.*)" ignoreCase="true" /> 
    <action type="Redirect" url="new_folder/{R:1}" /> 
</rule> 

測試它在我的本地機器上的文件夾,子文件夾與查詢字符串的請求。我希望這也適用於共享主機。

+0

謝謝。我同意你的重寫規則是正確的,應該做這項工作,但*在這臺服務器上*,重定向不起作用。我的問題是「我的規則是否正確?我的主人是否阻止我重新定向?如果是,我該怎麼說?」你的規則也是正確的,但這並不能幫助我理解**爲什麼這些規則不適用。 –

+0

共享主機的名稱是什麼?這是一個免費賬戶?共享主機通常意味着相同的服務器,而不是IIS域中的相同文件夾。 – VDWWD

+0

它屬於教職員工,我有一個帳戶,類型爲「http:// sharedhosting.com/myusername /」的子文件夾。 –