DNN Forums

Ask questions about your website to get help learning DNN and help resolve issues.

DNN Rewriting

Sort:
You are not authorized to post a reply.
Page 1 of 212 > >>





New Around Here





    Greetings,

    I am having trouble figuring out how to do a URL rewrite.

    These are the rules I currently have:

    <rewrite>
          <rules>
            <rule name="RedirectProjectURL" stopProcessing="true">
              <match url="^project$" />
              <conditions logicalGrouping="MatchAll">
                <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
                <add input="{QUERY_STRING}" pattern="^id=([^=&amp;]+)$" />
                <add input="{REQUEST_URI}" negate="true" pattern="^/project/([^/]+)/?$" />
              </conditions>
              <action type="Redirect" url="project/{C:1}" appendQueryString="false" />
            </rule>
            <rule name="RewriteProjectURL" stopProcessing="true">
              <match url="^project/([^/]+)/?$" />
              <conditions logicalGrouping="MatchAll">
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
              </conditions>
              <action type="Rewrite" url="project?id={R:1}" />
            </rule>
          </rules>
        </rewrite>

    The idea is to redirect all requests from "project?id=N" to "project/N" and then rewrite all "project/N" requests to "project?id=N". However, what currently happens is the request gets redirected to "project/id/N" instead of getting rewritten. My guess is that after the rewrite it matches a DNN rule of some sort and it gets redirected. But I don't know where and how I can fix that.

    Appreciate your help!






    Growing Member










      New Around Here





        I've read that article before I decided to ask here. All of those are for redirecting rather than rewriting. I am not 100% sure but I do believe the rules I have are correct, as before I had them "project/N" was not going to "project/id/N". I am missing something.






        Veteran Member





          Have you tried running the regex through any validators to be sure you have the correct regex?  (I don't have the time to check it myself at the moment.)

          https://regex101.com/






          New Around Here





            Yes, I ran it through the IIS regex testing tool. I also just ran it in regex101.com The regex seems to be working okay.






            Growing Member





              I'm not following your description of your idea. You seem to be wanting to redirect from A to B then rewrite from B back to A, but these are both in your inbound rules. Do you mean that you want DNN to natively process urls of the form "project?id=N", but accept urls of the form "project/N" and present links back to the end user in the form "project/N" ? If so, you'll need to set outbound rules.





              New Around Here





                Yes, the idea is for the user to see only "project/N", but for the server to process that as "project?id=N" so I can get the id parameter. Could you please explain why I need outbound rules and what they need to do? Or maybe direct me to an article that I could read about this? Thank you.






                Growing Member





                  Sorry for delay replying - I'm short of time and have not tested, but I think you need something like the code below. You shouldn't need to redirect, just rewrite the url on the way in to the server. When the server generates its response you need to change urls back to the format you want the user to see by using outbound rewrite rules.

                  <rewrite>
                  <rules>
                  <rule name="projectin" stopProcessing="true">
                  <match url="/project/([0-9]+)" />
                  <action type="Rewrite" url="/project?id={R:1}" />
                  </rule>
                  </rules>
                  <outboundRules>
                  <rule name="projectout" preCondition="IsHTML" stopProcessing="true">
                  <match pattern="/project\?id=([0-9]+)" />
                  <action type="Rewrite" value="/project/{R:1}" />
                  </rule>
                  <preConditions>
                  <preCondition name="IsHTML">
                  <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
                  </preCondition>
                  </preConditions>
                  </outboundRules>
                  </rewrite>





                  New Around Here





                    Thanks for the response! However, this still doesn't fix my issue.

                    Scrap the outbound rule - this is not the part I need help with. The problem I need fixing is that when the user requests a page with the format "project/N" he gets redirected to "project/id/N". I don't know where that redirect comes from, but I want to stop it.

                    It happens only when the inbound rule for the rewrite is active. I tried your rule and mine (I had to remove the / at the start from your for it to work) and both have the same result.

                    Please let me know if you have any other suggestions.






                    Veteran Member





                      I'm not sure I understand the issue correctly, but to me this seems as if DNN is rewriting the url for you.
                      Did you add an exception for the rewriting to SEO > Expressions > Ignore URL Regular Expression?

                      You are not authorized to post a reply.
                      Page 1 of 212 > >>

                      These Forums are dedicated to the discussion of DNN Platform.

                      For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

                      1. If you have (suspected) security issues, please DO NOT post them in the forums but instead follow the official DNN security policy
                      2. No Advertising. This includes the promotion of commercial and non-commercial products or services which are not directly related to DNN.
                      3. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
                      4. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
                      5. No Flaming or Trolling.
                      6. No Profanity, Racism, or Prejudice.
                      7. Site Moderators have the final word on approving / removing a thread or post or comment.
                      8. English language posting only, please.

                      Would you like to help us?

                      Awesome! Simply post in the forums using the link below and we'll get you started.

                      Get Involved