Simple url rewrite

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
7 years ago
I have been using my forum to post recipes that customers can use with my products.  I'd like to publish the location of this forum in a simple url on my brochures, labels, etc.  So how can I do a simple rewrite somewhere in the app?  I'd like people to be able to type www.anarchyacres.com/recipes into their browser and get to my recipe forum at http://www.anarchyacres.com/boards/forum/4/recipes.

I've tried url rewrite in IIS but I'm too dumb to make it work.  Maybe the rewrite should be in the application somewhere, preferably something easy like the .config file or global.asax.  I don't really care if it's a redirect or a rewrite, I just want people to easily find the forum topic.

Thanks for a great product!  MVC has been a steep learning curve for me but I love the application and hope to keep customizing my site.
7 years ago
[email protected] wrote:
I have been using my forum to post recipes that customers can use with my products.  I'd like to publish the location of this forum in a simple url on my brochures, labels, etc.  So how can I do a simple rewrite somewhere in the app?  I'd like people to be able to type www.anarchyacres.com/recipes into their browser and get to my recipe forum at http://www.anarchyacres.com/boards/forum/4/recipes.

I've tried url rewrite in IIS but I'm too dumb to make it work.  Maybe the rewrite should be in the application somewhere, preferably something easy like the .config file or global.asax.  I don't really care if it's a redirect or a rewrite, I just want people to easily find the forum topic.

Thanks for a great product!  MVC has been a steep learning curve for me but I love the application and hope to keep customizing my site.


You can just create a static rewrites file and upload to your website..it is a little tricky, but free.
In your web.config, you'll add a reference to a rewriteMap like this:

<system.webServer>
    <rewrite>
      <rewriteMaps configSource="StaticRedirects.config" />
      <rules>
        <rule name="non-www to www" enabled="true" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^[^\.]+\.[^\.]+$" />
          </conditions>
          <action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" />
        </rule>
        <rule name="Rewrites Redirect Rule">
          <match url=".*" />
          <conditions>
            <add input="{StaticRedirects:{REQUEST_URI}}" pattern="(.+)" />
          </conditions>
          <action type="Redirect" url="{C:1}" appendQueryString="False" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
</system.webServer>


That StaticRedirects.config file will be structured something like this:
The "key" is the old address and the "value" is the new address.

<rewriteMaps>
       <rewriteMap name="StaticRedirects" defaultValue="">
           <add key="/detail.asp?ProductID=948" value="/3pp-arch-lift" />
           <add key="/detail.asp?ProductID=830" value="/3pp-plantar-fasciitis-lift" />
           <add key="/detail.asp?ProductID=864" value="/3pp-toe-loops" />
           <add key="/detail.asp?ProductID=873" value="/achilles-tendon-support-by-pro-tec" />
           <add key="/detail.asp?ProductID=856" value="/airheel-by-aircast-2" />
           <add key="/detail.asp?ProductID=858" value="/malleoloc-ankle-brace" />
           <add key="/detail.asp?ProductID=789" value="/ankle-support-elastic-1" />
</rewriteMap>
</rewriteMaps>



You just need to create an entry for each of your old addresses so as to inform the search engines about the permanent redirects to new addresses.
7 years ago
Thanks!  That did the trick.  For future guidance, here is the full text of StaticRedirects.config that ended up working for me:

<?xml version="1.0"?>
  <rewriteMaps>
    <rewriteMap name="StaticRedirects" defaultValue="">
      <add key="/recipes" value="/boards/forum/4/recipes" />
    </rewriteMap>
  </rewriteMaps>

Kind of tricky to get it just right--got a lot of 404's and then I discovered that chrome actually memorizes the redirect, so changing the rewrite map afterwards may not be possible if users have already used the redirect.
6 years ago
embryo wrote:
I have been using my forum to post recipes that customers can use with my products.  I'd like to publish the location of this forum in a simple url on my brochures, labels, etc.  So how can I do a simple rewrite somewhere in the app?  I'd like people to be able to type www.anarchyacres.com/recipes into their browser and get to my recipe forum at http://www.anarchyacres.com/boards/forum/4/recipes.

I've tried url rewrite in IIS but I'm too dumb to make it work.  Maybe the rewrite should be in the application somewhere, preferably something easy like the .config file or global.asax.  I don't really care if it's a redirect or a rewrite, I just want people to easily find the forum topic.

Thanks for a great product!  MVC has been a steep learning curve for me but I love the application and hope to keep customizing my site.

You can just create a static rewrites file and upload to your website..it is a little tricky, but free.
In your web.config, you'll add a reference to a rewriteMap like this:

<system.webServer>
    <rewrite>
      <rewriteMaps configSource="StaticRedirects.config" />
      <rules>
        <rule name="non-www to www" enabled="true" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^[^\.]+\.[^\.]+$" />
          </conditions>
          <action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" />
        </rule>
        <rule name="Rewrites Redirect Rule">
          <match url=".*" />
          <conditions>
            <add input="{StaticRedirects:{REQUEST_URI}}" pattern="(.+)" />
          </conditions>
          <action type="Redirect" url="{C:1}" appendQueryString="False" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
</system.webServer>


That StaticRedirects.config file will be structured something like this:
The "key" is the old address and the "value" is the new address.

<rewriteMaps>
       <rewriteMap name="StaticRedirects" defaultValue="">
           <add key="/detail.asp?ProductID=948" value="/3pp-arch-lift" />
           <add key="/detail.asp?ProductID=830" value="/3pp-plantar-fasciitis-lift" />
           <add key="/detail.asp?ProductID=864" value="/3pp-toe-loops" />
           <add key="/detail.asp?ProductID=873" value="/achilles-tendon-support-by-pro-tec" />
           <add key="/detail.asp?ProductID=856" value="/airheel-by-aircast-2" />
           <add key="/detail.asp?ProductID=858" value="/malleoloc-ankle-brace" />
           <add key="/detail.asp?ProductID=789" value="/ankle-support-elastic-1" />
</rewriteMap>
</rewriteMaps>



You just need to create an entry for each of your old addresses so as to inform the search engines about the permanent redirects to new addresses.





I read the report. But my file is bigger than 250KB.

Do you have an example for me how I can solve my problem.

NopCommerce Version 4.00 and 3.90
Windows Server 2016
6 years ago
Maximum size of a web.config allowed is 250KB by default. You can change regkey HKLM\SOFTWARE\Microsoft\InetStp\Configuration\MaxWebConfigFileSizeInKB to allow bigger web.config if needed.

As described in my initial post, you can get around that by creating a separate redirect file so that your web.config filesize is not affected.

<system.webServer>
    <rewrite>
      <rewriteMaps configSource="StaticRedirects.config" />
      <rules>
        <rule name="non-www to www" enabled="true" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^[^\.]+\.[^\.]+$" />
          </conditions>
          <action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" />
        </rule>
        <rule name="Rewrites Redirect Rule">
          <match url=".*" />
          <conditions>
            <add input="{StaticRedirects:{REQUEST_URI}}" pattern="(.+)" />
          </conditions>
          <action type="Redirect" url="{C:1}" appendQueryString="False" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
</system.webServer>


And the StaticRedirects.config file will map all your new URLs:

<?xml version="1.0"?>
  <rewriteMaps>
    <rewriteMap name="StaticRedirects" defaultValue="">
      <add key="/recipes" value="/boards/forum/4/recipes" />
      <add key="/aboutus" value="/about" />
    </rewriteMap>
  </rewriteMaps>



Read more here:
http://ruslany.net/2010/05/storing-url-rewrite-mappings-in-a-separate-file/
6 years ago
embryo wrote:
Maximum size of a web.config allowed is 250KB by default. You can change regkey HKLM\SOFTWARE\Microsoft\InetStp\Configuration\MaxWebConfigFileSizeInKB to allow bigger web.config if needed.

As described in my initial post, you can get around that by creating a separate redirect file so that your web.config filesize is not affected.

<system.webServer>
    <rewrite>
      <rewriteMaps configSource="StaticRedirects.config" />
      <rules>
        <rule name="non-www to www" enabled="true" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^[^\.]+\.[^\.]+$" />
          </conditions>
          <action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" />
        </rule>
        <rule name="Rewrites Redirect Rule">
          <match url=".*" />
          <conditions>
            <add input="{StaticRedirects:{REQUEST_URI}}" pattern="(.+)" />
          </conditions>
          <action type="Redirect" url="{C:1}" appendQueryString="False" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
</system.webServer>


And the StaticRedirects.config file will map all your new URLs:

<?xml version="1.0"?>
  <rewriteMaps>
    <rewriteMap name="StaticRedirects" defaultValue="">
      <add key="/recipes" value="/boards/forum/4/recipes" />
      <add key="/aboutus" value="/about" />
    </rewriteMap>
  </rewriteMaps>



Read more here:
http://ruslany.net/2010/05/storing-url-rewrite-mappings-in-a-separate-file/



Thanks for the answer.

Under Windows Server 2016, there is only "SOFTWARE\Microsoft\InetStp\Components" but not "SOFTWARE\Microsoft\InetStp\Configuration". Does "SOFTWARE\Microsoft\InetStp\Components" need to be created?
Does the decimal value have to be entered in kb?
6 years ago
I did that. It does not work with Windows Server 2016 64bit.

the value is newly created: Configuration\MaxWebConfigFileSizeInKB and REG_DWORD(1024)


HKLM\SOFTWARE\Wow6432Node\Microsoft\InetStp\Configuration\MaxWebConfigFileSizeInKB (REG_DWORD)
5 years ago
Hi,

I have just tried to do this for 4.0 but it throws an error. Do I need to do it differently for 4.0, or am I just doing something wrong?

Thanks
Neil
5 years ago
If you have a large number of URLs, I strongly recommend using FileMapProvider instead using rewriteMaps.  It allows you to have a large file full of URLS without the XML formatting requirement.  Simply create a {DOCUMENT_ROOT}\App_Data\OldUrlsRedirectMappings.txt file and add entries:

/old_url/product_1    /new_url/product-1


You also have an option for setting the delimiter, in the example it's just whitespace.

It requires an IIS module installation.  Make sure you install the module and restart IIS to ensure it is properly loaded.  Also, make sure your urls paths start with a '/', otherwise they won't be matched.  I discovered this the hard way.  Finally, make sure you have .NET 4 along with integrated pipeline or you'll get a 500 server error.

The following has installation instructions and where you can download the module:
https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/using-custom-rewrite-providers-with-url-rewrite-module

The following Rewrite

    <rewrite>
      <rules>
        <clear />
        <rule name="RemapOldUrls" enabled="true" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
                        <add input="{FileMap:{R:1}}" pattern="(.+)" />
          </conditions>
          <action type="Redirect" url="{C:1}" appendQueryString="false" />
        </rule>
      </rules>
      <providers>
        <provider name="FileMap" type="FileMapProvider, Microsoft.Web.Iis.Rewrite.Providers, Version=7.1.761.0, Culture=neutral, PublicKeyToken=0545b0627da60a5f">
          <settings>
            <add key="FilePath" value="{DOCUMENT_ROOT}\App_Data\OldUrlsRedirectMappings.txt" />
            <add key="IgnoreCase" value="1" />
          </settings>
        </provider>
      </providers>
    </rewrite>
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.