2

I have a webpage hosted on a shared Microsoft-IIS/8.5 server. 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.

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, i.e., at http://sharedhosting.com/myusername/). I placed in the root folder the following web.config file:

<?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. (if I leave an empty old_folder) or a 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

<?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). I suspect that the URL rewrite is installed, since, for instance, http://sharedhosting.com/myusername/folder is rewritten to http://sharedhosting.com/myusername/folder/ (i.e., with a slash) if the folder folder exists.

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.

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.

Clément
  • 2,358
  • 28
  • 32

2 Answers2

3

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 .

Here is a sample httphandlers , you can do in this way :

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:

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

Using 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>
Krsna Kishore
  • 8,233
  • 4
  • 32
  • 48
  • @Clément update your question with how you tried ... its hard to read in comments – Krsna Kishore Sep 18 '17 at 04:48
  • @Clément i have provided 2 solutions , one with the `IIS url` rewriting another one with `http handlers` – Krsna Kishore Sep 18 '17 at 05:35
  • I'm sorry but I have absolutely no idea how to use `httphandlers`. I'm not familiar with `asp.net`, so I'm trying to test, but I can't even tell if your solution involving `httphandlers` is working or not. As I said, the solution involving `web.config` isn't working, I'm quite positive. Thanks a lot for your assistance, I appreciate it. – Clément Sep 18 '17 at 05:41
  • @Clément , ah, np , will try to assit you .. first please tell me your asp.net framework , is it in `4.6` or `4.5` ,, you will get to know this version number in your webconfig – Krsna Kishore Sep 18 '17 at 05:51
  • Thanks. Again, I'm not root, so all I can do is try and see. I created a `.aspx` file with `<%= System.Environment.Version.ToString() %> ` in it, uploaded it (I know the file is correctly uploaded), but when I try to access it from my browser, I get a `404.0` error. Could there be a filter or module that restricts access to `.aspx` files? – Clément Sep 18 '17 at 13:03
  • @Clément , ah that is a typical one to guess without seeing the environment . ok , you will be having a development environment right , don't you have the access to the files? – Krsna Kishore Sep 18 '17 at 13:07
  • I have access to the files through `ftp`, but I'm not sure what you mean by "development environment". – Clément Sep 18 '17 at 13:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/154690/discussion-between-webruster-and-clement). – Krsna Kishore Sep 18 '17 at 13:10
  • Even if your solution didn't worked, I appreciated the level of details you provided, and hence awarded you the bounty. Thanks. – Clément Sep 25 '17 at 01:02
3

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. If you want the url to change it's display to new_folder in it, use

<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. I hope this works on shared hosting also.

VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • Thanks. I agree that your rewrite rule is correct and should do the job, but *on this server*, the redirection doesn't work. My question was "Is my rule correct? Does my host prevent me from redirecting? If yes, how can I tell?" Your rule is also correct, but that doesn't help me in understanding **why** those rules are not applied. – Clément Sep 21 '17 at 15:16
  • What is the name of the shared hoster? And is it a free account? Shared hosting usually means the same server, not the same folder within an IIS domain. – VDWWD Sep 21 '17 at 15:56
  • It belongs to a faculty, I have an account, a sub-folder of the type `http://sharedhosting.com/myusername/`. – Clément Sep 21 '17 at 16:17
  • Ah, no way to test myself :( But what if you try `^username/old_folder/` > `username/new_folder/{R:1}`? maybe that will work... – VDWWD Sep 21 '17 at 16:24
  • Good try, but no `;-)` – Clément Sep 21 '17 at 16:31