简体   繁体   English

在共享主机上忽略了web.config重定向

[英]web.config redirection ignored on a shared hosting

I have a webpage hosted on a shared Microsoft-IIS/8.5 server. 我有一个网页托管在共享的Microsoft-IIS / 8.5服务器上。 I don't have the admin privileges on that server, and I don't have access to its configuration files, so I can't really make sure that the URL rewrite module or the HTTP redirect element are correctly installed / set up. 我在该服务器上没有管理员权限,也无法访问其配置文件,因此我无法真正确保正确安装/设置了URL重写模块或HTTP重定向元素。

I would like to establish a redirection from a folder named old_folder to a folder named new_folder (both at the root for me, ie, at http://sharedhosting.com/myusername/ ). 我想建立一个从名为old_folder的文件夹到名为old_folder的文件夹的new_folder (都在我的根目录下,即http://sharedhosting.com/myusername/ )。 I placed in the root folder the following web.config file: 我在根文件夹中放置了以下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>

But all I can get is a 403 - Forbidden: Access is denied. 但是我只能得到403 - Forbidden: Access is denied. (if I leave an empty old_folder ) or a 404 - File or directory not found. (如果我留下一个空的old_folder )或404 - File or directory not found. (if I erase it). (如果我将其擦除)。

I tried using the HTTP Redirect , by placing in old_folder a Web.config file containing 我尝试使用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> 

But that didn't worked either. 但这也不起作用。

I know that my Web.config files are read (I can get 500 errors if there is an error in them). 我知道我的Web.config文件已被读取(如果其中有错误,我会得到500个错误)。 I suspect that the URL rewrite is installed, since, for instance, http://sharedhosting.com/myusername/folder is rewritten to http://sharedhosting.com/myusername/folder/ (ie, with a slash) if the folder folder exists. 我怀疑是安装了URL重写,因为,例如, http://sharedhosting.com/myusername/folder改写为http://sharedhosting.com/myusername/folder/如果(即,以斜杠) folder文件夹存在。

Is my rule correct? 我的规则正确吗? Does my host prevent me from redirecting? 我的房东会阻止我重定向吗? If yes, how can I tell? 如果可以,我该如何判断?

I added a <clear /> after the <rules> tag to discard all the other rewriting rules, but it still isn't redirecting anything. 我在<rules>标记后添加了<clear />以丢弃所有其他重写规则,但是它仍然没有重定向任何内容。

What am I missing? 我想念什么?

LAST EDIT BEFORE THE BOUNTY EXPIRES To clarify, my question is "How to understand why the server isn't correctly interpreting / ignoring my instructions?". 赏金到期前的最后编辑要澄清的是,我的问题是“如何理解为什么服务器未正确解释/忽略我的指令?”。

We came to the conclusion that it was probably my server that was weirdly set-up, I'm precisely trying to "revert-engineer" that and to detect what's making the server ignore the redirect / rewrite rules. 我们得出的结论是,可能是我的服务器设置异常,我正试图对它进行“还原工程”,并检测导致服务器忽略重定向/重写规则的原因。

You can achieve the same thing by using the HttpHandlers , so i'm presuming that you can access the both Old_folder and New_folder directly in the browser . 您可以通过使用HttpHandlers实现相同的操作,因此我假设您可以直接在浏览器中访问Old_folderNew_folder

Here is a sample httphandlers , you can do in this way : 这是一个示例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; }
     } 
   }
}

In web_config add the handler as follows: web_config添加处理程序,如下所示:

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

Using IIS Url_rewriting : 使用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>

I think this can be done with a simple rewrite rule. 我认为可以通过简单的重写规则来完成。

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

The above rule will send all requests to the new folder, but keeps the old url with old_folder in it. 上面的规则会将所有请求发送到新文件夹,但是保留带有old_folder的旧网址。 If you want the url to change it's display to new_folder in it, use 如果您想更改网址,将其显示为new_folder ,请使用

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

Tested it on my local machine with folders, subfolders an requests with a QueryString. 在我的本地计算机上使用文件夹对其进行了测试,并使用QueryString将请求子文件夹。 I hope this works on shared hosting also. 我希望这也适用于共享主机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM