How to rewrite Query String URLs

Nov 19, 2013 at 4:55 AM
Edited Nov 19, 2013 at 5:05 AM
i am using DNN 6, I have urls like below,

http://localhost/Training/rpscategory.aspx?P=Oracle&Cat=Middleware

I want to rewrite above url like,

http://localhost/Training/Oracle/Middleware

..
and

..

http://localhost/Training/rpscategory.aspx?P=Oracle&Cat=Middleware&Course=Monitor-and-Performance


I want to rewrite above url like,

http://localhost/Training/Oracle/Middleware/Monitor-and-Performance


How to Achieve this by OpenUrlRewriter


Thanks in Advance
Coordinator
Nov 19, 2013 at 8:44 AM
Edited Nov 19, 2013 at 9:29 AM
Hi,

To do what you want, you have to create a new provider.

You have to be a developer. Is it the case ?

More info :

https://openurlrewriter.codeplex.com/wikipage?title=Implementing%20a%20new%20provider&referringTitle=Documentation

Also interesting :

https://openurlrewriter.codeplex.com/discussions/447470

Regards,
Sacha
Nov 19, 2013 at 9:32 AM
Hi, Sacha


Thanks for Reply...

I'm a developer...


Can u guide me..

I already checked both two link,

in which class i have to update GetRules() function,

do i have to open source code and change their or in my project i can update that method..
Nov 19, 2013 at 9:41 AM
Edited Nov 19, 2013 at 9:47 AM
I have added new url rules,


Url: .*/Training/(.)/(.*)

Redirect destination: http://localhost/rpstraining/rpscategory.aspx?CatId=$1&Type=category

Redirect Status: 302

While giving on browser, http://localhost/rpstraining/Training/52/Category

Its again redirecting to http://localhost/rpstraining/rpscategory?CatId=52&Type=category page
Coordinator
Nov 19, 2013 at 10:40 AM
Edited Nov 19, 2013 at 10:42 AM
Hi,

Redirect rules are 1 direction only. And are to use to redirect "bad pages" to good one. I mean for SEO.

A provider give you 2 direction rewrite (url generating in the pages and rewriting incoming url).

How to create a provider.

1) Create a directory [modulename] in app_code
2) Create a class in app_code/[modulename]

Here an exemple content :
namespace Satrabel.OpenUrlRewriter.DNNSimpleArticle
{
    public class DNNSimpleArticleUrlRuleProvider : UrlRuleProvider
    {

        public override List<UrlRule> GetRules(int PortalId)
        {
            List<UrlRule> Rules = new List<UrlRule>();

            ModuleController mc = new ModuleController();
            ArrayList modules = mc.GetModulesByDefinition(PortalId, "dnnsimplearticle");

            foreach (ModuleInfo module in modules.OfType<ModuleInfo>())
            {
                var articles = ArticleController.GetArticles(module.ModuleID);
                
                
                foreach (var article in articles)
                {
                    var rule = new UrlRule
                    {
                        CultureCode = module.CultureCode,
                        TabId = module.TabID,
                        RuleType = UrlRuleType.Module,
                        Parameters = "aid="+article.ArticleId.ToString(),
                        Action = UrlRuleAction.Rewrite,
                        Url = CleanupUrl(article.Title),
                        RemoveTab = false
                    };                    
                    Rules.Add(rule);                    
                }
            }             
            return Rules;
        }

    }
}
You have to create 1 UrlRule for each url of your module.

In your exemple :
rule.Parameters = P=Oracle&Cat=Middleware&Course=Monitor-and-Performance
rule.Url = Oracle/Middleware/Monitor-and-Performance

rspcategory is a pagename ?
If yes to remove it : RemoveTab = true and add Trainng to the rule.url


3) add the provider to web.config
<urlRule defaultProvider="tabUrlRuleProvider">
  <providers>
    <clear/>
...
    <add name="myModuleUrlRuleProvider" type="MyNameSpace.MyModuleUrlRuleProvider"/>

...
  </providers>
</urlRule>
It's all.

Check the generated url rules in Admin/Open Url Rweriter, view Rules in cache.

Regards,
Sacha
Nov 19, 2013 at 10:47 AM
Edited Nov 19, 2013 at 12:30 PM
Yes, RpsCategory is page name,

I am doing following steps but still its not working,

Step1. Added "mymodule" folder in AppCode

Step2. Added in web.config
<compilation debug="true" strict="false">
      <codeSubDirectories>
        <add directoryName="mymodule"/>
      </codeSubDirectories>
    </compilation>
Step3. Created a class "MyCustomUrlRuleProvider.cs", inside mymodule folder, written logic inside that
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Collections.Generic;
using DotNetNuke.Common.Utilities;

using DotNetNuke.Entities.Modules;
using DotNetNuke.Framework.Providers;
using Satrabel.HttpModules.Provider;
using DotNetNuke.Entities.Tabs;
using RPSTraining_DAL;

namespace DotNetNuke.App_Code.mymodule
{
    /// <summary>
    /// Summary description for MyCustomUrlRuleProvider
    /// </summary>
    public class MyCustomUrlRuleProvider : UrlRuleProvider
    {
        public MyCustomUrlRuleProvider()
        {
         }

        public override List<UrlRule> GetRules(int PortalId)
        {
            List<UrlRule> Rules = new List<UrlRule>();

            ModuleController mc = new ModuleController();
            ArrayList modules = mc.GetModulesByDefinition(PortalId, "dnnsimplearticle");

            Category_DAL cDAL = new Category_DAL();


            foreach (ModuleInfo module in modules.OfType<ModuleInfo>())
            {
                DataTable dtCatIds = cDAL.GetAllCategory();


                foreach (DataRow dr in dtCatIds.Rows)
                {
                    var rule = new UrlRule
                    {
                        CultureCode = module.CultureCode,
                        TabId = module.TabID,
                        RuleType = UrlRuleType.Custom,
                        Parameters = "CatId="&#43;dr["CatID"].ToString()&#43;"&Type=Category",
                        Action = UrlRuleAction.Rewrite,
                       Url = CleanupUrl(dr["CatID"].ToString()) + "/" + CleanupUrl("Category"),
                        RemoveTab = true
                    };
                    Rules.Add(rule);
                }
            }
            return Rules;
        }


    }
}

Step4. Added provider to web.config
<urlRule defaultProvider="tabUrlRuleProvider">
      <providers>
        <clear />
        <add name="tabUrlRuleProvider" type="Satrabel.HttpModules.Provider.TabUrlRuleProvider, Satrabel.OpenUrlRewriter" useKeyWords="true" />
        <add name="userUrlRuleProvider" type="Satrabel.HttpModules.Provider.UserUrlRuleProvider, Satrabel.OpenUrlRewriter" />
        <add name="languageUrlRuleProvider" type="Satrabel.HttpModules.Provider.LanguageUrlRuleProvider, Satrabel.OpenUrlRewriter" />
        <add name="ctlUrlRuleProvider" type="Satrabel.HttpModules.Provider.CtlUrlRuleProvider, Satrabel.OpenUrlRewriter" />
        <add name="newsArticlesUrlRuleProvider" type="Satrabel.OpenUrlRewriter.NewsArticles.NewsArticlesUrlRuleProvider, Satrabel.OpenUrlRewriter.NewsArticles" includePageName="true" />
        <add name="dnnMyCustomUrlRuleProvider" type="DotNetNuke.App_Code.mymodule.DNNSimpleArticleUrlRuleProvider"/>
      </providers>
    </urlRule>
But still my page is like this,

http://localhost/RPSTraining/rpscategory?CatId=51&Type=category Its didn't rewritten...
Nov 19, 2013 at 1:17 PM
I am getting my Category by a dll project,

that function is

public DataTable GetAllCategory()
    {
        DataTable dt = new DataTable();
        try
        {
            dt = DBWizard.FillDataTable(conn, "SP_GetAllCategory");
            return dt;
        }
        catch
        {
            return dt;
        }
    }
how to create controller to get categories
Nov 19, 2013 at 2:09 PM
Hi, Sacha


Please check my code, where i am going wrong...
Coordinator
Nov 19, 2013 at 3:51 PM
Hi,

1) I see an error on
 <add name="dnnMyCustomUrlRuleProvider" type="DotNetNuke.App_Code.mymodule.DNNSimpleArticleUrlRuleProvider"/>
DNNSimpleArticleUrlRuleProvider is not your class name

it must be

type="DotNetNuke.App_Code.mymodule.MyCustomUrlRuleProvider"

2) ArrayList modules = mc.GetModulesByDefinition(PortalId, "dnnsimplearticle");

You must replace dnnsimplearticle with your module fliendlyname (look in Host, Extentions)

3) RuleType = UrlRuleType.Custom
You must use Module in place of custom

4) Replace + with +
(I think is just a codeplex rendering problem)

5) parameters and Url
if this are the real thigs

http://localhost/Training/rpscategory.aspx?P=Oracle&Cat=Middleware
I want to rewrite above url like,
http://localhost/Training/Oracle/Middleware You need somethink like rule.Parameters = "P="+dr["p"]+"&Cat="+dr["Cat"]
rule.Url = "training/" + dr["p"]+"/"+dr["Cat"]

In parameters you need to put the full string which comes after the pagename in the url.

Regards,
Sacha
Nov 20, 2013 at 11:19 AM
Thanks a lot Sacha, Its working fine....
Nov 20, 2013 at 1:28 PM
hi, Sacha

While typing this on browser,
..
http://localhost/Training/rpscategory.aspx?P=Oracle&Cat=Middleware
..
Its not converting to
..
http://localhost/Training/Oracle/Middleware
..

i have to change all url's manually in my website??

Or there is a way that'll automatic send
this url
..
http://localhost/Training/rpscategory.aspx?P=Oracle&Cat=Middleware
..
to this url
..
http://localhost/Training/Oracle/Middleware
..
Coordinator
Nov 21, 2013 at 8:49 AM
Hi,

OpenUrlRewriter can do redirections from old url to new urls.

But actually only from path part of the url not of the querystring part.

Exemple :
From
http://localhost/Training/rpscategory/P/Oracle/Cat/Middleware to http://localhost/Training/Oracle/Middleware Because in DNN if you use NavigateUrl you never have querystrings.

For your case, you can try to add a custom rule (you do in the admin screen)

Redirect 301
from
./Training/rpscategory.aspx?P=(.)&Cat=(*.)
to
http://localhost/Training/$1/$2

Regards,
Sacha
Nov 26, 2013 at 11:34 AM
Extra "/" is creating in url...


http://fbpages.biz//training/oracle

How to remove that extra "/"

My Code is:
 foreach (DataRow dr in dtPartners.Rows)
                {
                    var rule = new UrlRule
                    {
                        CultureCode = module.CultureCode,
                        TabId = module.TabID,
                        RuleType = UrlRuleType.Module,
                        Parameters = "PartnerId=" + dr["PartnerID"].ToString(),
                        Action = UrlRuleAction.Rewrite,
                        Url = "training/" + CleanupUrl(dr["Name"].ToString()),
                        RemoveTab = true
                    };
                    Rules.Add(rule);
                }
Coordinator
Nov 26, 2013 at 1:40 PM
Hi,

I don't see directly a reasop for it. I check it out.

But can you try :

Url = CleanupUrl(dr["Name"].ToString()),
RemoveTab = false


I think, that it give a little but the same.

I see also other thinks on your site witch are not perfect :

1) The urls of the links in your breadclumb are in uppercase
2) links of your trainings are with partnerid/1

This generates redirects, which normally don't happend.

Do you use Globals.NagigtateUrl for generating the urls ?

Regards,
Sacha
Nov 27, 2013 at 9:03 AM
Edited Nov 27, 2013 at 10:07 AM
Hi, sacha


every urls i changed to lower case...


if i am giving RemoveTab=false

Then its not adding two "/" in url

http://www.rpssoft.urmail.net/rpstraining/training/oracle

But i dont want pagename in url...

i am thinking, its adding extra "/" because of RemoveTab, its removing tab but its not removing tab "/".....



i am not using Globle.navigateURl also....



Image
Coordinator
Nov 27, 2013 at 10:27 PM
i am thinking, its adding extra "/" because of RemoveTab, its removing tab but its not removing tab "/".....
You are right !
I post corrected version : Open Url Rewriter 1.1.1 Beta

Can you tell me if it correct your problem.

I will also tell you that it was very very better if you use Globals.NavigateUrl .

Because now all link generate all the times redirections.
This make your site slower.
And you give also a bad information to Google, because google see your urls in the links, and after google see that it is a redirection and will correct the index but the link not really exist on the pages. How more simply it is for google to understand your site structure (links) , better he will index you.

Regards,
Sacha
Nov 28, 2013 at 6:04 AM
Hi, Sacha

Thanks for your reply...

I checked "Open Url Rewriter 1.1.1 Beta", Its working fine...

Now its not adding extra "/" in url...

Can u guide me how to use Globals.NavigateUrl

i have to create NavigateUrl function in my Global.asax ???
Coordinator
Nov 28, 2013 at 6:49 AM
Hi,

The only to you have to do is, when you create url for a link is to construct the url with the Globals.NavigateUrl .

Exemple :

For this
Url = http://localhost/Training/rpscategory.aspx?P=Oracle&Cat=Middleware

You do actually something like this (i suppose)

Url = "/Training/rpscategory.aspx" + "?" + "P=" + productName + "&Cat=" + categoryName

You have to replace it with

Url = Globals.NavigateUrl (TabID, "", new string[] { "P=" + productName, "Cat=" + categoryName} );

You need also to add using DotNetNuke.Common; in the usings.


When you to this DNN call the FriendlyUrlProvider (Open Url in this case) to generate the url.

Regards,
Sacha