Post URLs + category confusion

Topics: ASP.NET 2.0, Business Logic Layer, Controls, Themes
Aug 12, 2011 at 9:10 PM

Hi,

I'm having a really hard time trying to figure this out and I'm sure it's a simple setting or fix somewhere.

I've installed BE 2.5 in a subfolder at mydomain.com/blog and have created a new blog.

I've also created my categories.

In the settings I've deselected the "Add date to post links" to stop the post from looking something like this:

http://www.mydomain.com/blog/post/2011/8/12/name-of-post.aspx

and have it looking like:

http://www.mydomain.com/blog/post/name-of-post.aspx

but what I really want is it to look like this instead

http://www.mydomain.com/categoryname/name-of-post.aspx

Is there an easy way to do this please?

Thank you!

Jason

Aug 13, 2011 at 1:15 AM
Edited Oct 24, 2011 at 7:38 PM

Need to recode the url writing in BE

 

Post is here: http://blogengine.codeplex.com/discussions/242914

Replace Page with Post  and used the Post.cs instead of the Page.cs :)

It says below:

In the BE core, there is a Page.cs file.  In there, is a property called RelativeLink.  In BE 2.0, it looks like this:

 

Now getting the categoryname in there is a little trickly not sure on that one and so far

no one has posted anything on that :)

public string RelativeLink
{
	get
	{
		var theslug = Utils.RemoveIllegalCharacters(this.Slug) + BlogSettings.Instance.FileExtension;
		return string.Format("{0}page/{1}", Utils.RelativeWebRoot, theslug);
	}
}

You can remove page/ from that.

The 2nd part is in UrlRewrite.cs (also in the BE core) in the /Web/HttpModules folder.  In there, it's checking to see if the URL contains /page/.  The code looks like:

else if (url.Contains("/PAGE/"))
{
	RewritePage(context, url);
}

This part is a little trickier.  That code probably needs to be moved a little lower down, and if none of the other IF statements make a match, then you could try searching thru the pages to see if there's a matching page name.  RewritePage() checks for a page, and only does something if a page is found.  So you might even be able to move that code above down to the bottom, so it looks like:

else
{
	RewritePage(context, url);
 }



 

Java Blog

Aug 13, 2011 at 6:33 PM

Thanks for your help...

I had no problems with the first part... but the second part is a little trickier as mentioned.

I'm not exactly sure where to move the code to? Down to the bottom? I ok with VB but really am having a hard time with C#.

In URLrewrite.cs I changed it so it looks like this:

             //remove the reference to "/post/"            
//if (urlContainsFileExtension && url.Contains("/POST/")) { // RewritePost(context, url); //} //else if (urlContainsFileExtension && url.Contains("/CATEGORY/")) {

            if (urlContainsFileExtension && url.Contains("/CATEGORY/")) {

                  RewriteCategory(context, url);
            }
            else if (urlContainsFileExtension && url.Contains("/TAG/")) {
                RewriteTag(context, url);
            }
            else if (urlContainsFileExtension && url.Contains("/PAGE/")) {
                RewritePage(context, url);
            }
            else if (urlContainsFileExtension && url.Contains("/CALENDAR/")) {
                context.RewritePath(string.Format("{0}default.aspx?calendar=show", Utils.ApplicationRelativeWebRoot), false);
            }
            else if (urlContainsFileExtension && url.Contains(string.Format("/DEFAULT{0}", BlogConfig.FileExtension.ToUpperInvariant()))) {
                RewriteDefault(context);
            }
            else if (urlContainsFileExtension && url.Contains("/AUTHOR/")) {
                var author = ExtractTitle(context, url);
                context.RewritePath(
                    string.Format("{0}default{1}?name={2}{3}", Utils.ApplicationRelativeWebRoot, BlogConfig.FileExtension, author, GetQueryString(context)),
                    false);
            }
            else if (urlContainsFileExtension && path.Contains("/BLOG.ASPX")) {
                context.RewritePath(string.Format("{0}default.aspx?blog=true{1}", Utils.ApplicationRelativeWebRoot, GetQueryString(context)));
            }
            else {
                // If this is blog instance that is in a virtual sub-folder, we will
                // need to rewrite the path for URL to a physical file.  This includes
                // requests such as the homepage (default.aspx), contact.aspx, archive.aspx,
                // any of the admin pages, etc, etc.

                if (blogInstance.IsSubfolderOfApplicationWebRoot &&
                    VirtualPathUtility.AppendTrailingSlash(path).IndexOf(blogInstance.RelativeWebRoot, StringComparison.OrdinalIgnoreCase) != -1) {
                    bool skipRewrite = false;
                    string rewriteQs = string.Empty;
                    string rewriteUrl = GetUrlWithQueryString(context);

                    int qsStart = rewriteUrl.IndexOf("?");
                    if (qsStart != -1)  // remove querystring.
                    {
                        rewriteQs = rewriteUrl.Substring(qsStart);
                        rewriteUrl = rewriteUrl.Substring(0, qsStart);
                    }

                    // Want to see if a specific page/file is being requested (something with a . (dot) in it).
                    // Because Utils.ApplicationRelativeWebRoot may contain a . (dot) in it, pathAfterAppWebRoot
                    // tells us if the actual path (after the AppWebRoot) contains a dot.
                    string pathAfterAppWebRoot = rewriteUrl.Substring(Utils.ApplicationRelativeWebRoot.Length);

                    if (!pathAfterAppWebRoot.Contains(".")) {
                        if (!rewriteUrl.EndsWith("/"))
                            rewriteUrl += "/";

                        rewriteUrl += "default.aspx";
                    }
                    else {
                        if (Path.GetExtension(pathAfterAppWebRoot).ToUpperInvariant() == ".AXD")
                            skipRewrite = true;
                    }

                    if (!skipRewrite) {
                        // remove the subfolder portion.  so /subfolder/ becomes /.
                        rewriteUrl = new Regex(Regex.Escape(blogInstance.RelativeWebRoot), RegexOptions.IgnoreCase).Replace(rewriteUrl, Utils.ApplicationRelativeWebRoot);

                        context.RewritePath(rewriteUrl + rewriteQs, false);
                    }

                    return;

But I'm not sure where to insert the

else
{
	RewritePage(context, url);
 }

Apologies for what is probably a simple thing for savvy C# guys.

Any help is greatly appreciated.

Thank you,

Jason

Aug 13, 2011 at 8:12 PM

This is the URL format you want right:

http://dev3.kbdavis07.info/For-Sale/TransportChair.aspx

 

Got to have code that checks the post if it have any Category<-----Not Categories :)

if it does

then rewrite the url

to  category/Post URL

else

do normal url rewrite.

But I think this is only going to work with 1 category per post through.

If you have more than one then its going to write multiple URLs for the same post

then your SEO will go down for having duplicate URLS :)

 

So all of your posts can only have 1 category is this what you want?

 

Hope this helps,

 

Brian Davis

Aug 15, 2011 at 5:00 PM

Hi Brian,

Thanks for your help so far...

Yes, I basically wanted the URL format like:

http://www.mysite.com/mycategory/my-post.aspx

One category per post is fine.

The problem I'm having however is trying to modify the code to do that as per my last message. As a simple test, I tried to just remove the "post/" from the code as per the example, and not worry about the category for the moment.

What's happening is the URL is changing ok when I mouse over the link, but when I try to edit the post, the server returns a 404 not found for the format http://www.mysite.com/my-post.aspx. Obviously I'm going wrong somewhere in the URLrewrite file.

Not sure where to move the code below to?

else
{
	RewritePage(context, url);
 }

Aug 15, 2011 at 9:29 PM
Edited Aug 15, 2011 at 9:44 PM

 

Post URL Rewrite For BlogEngine.NET v2.0 In Two Easy Steps

 

Here you go:  http://www.blogenginewall.com/post/2011/04/09/Post-URL-Rewrite-For-BlogEngineNET-v20-In-Two-Easy-Steps.aspx

 

This will get you in the right direction but its only for BE 2.0

 

Comment on the post: 

 

This is only for 2.0 version not made for 2.5. There are various changes in 2.5 regarding URL re-write.

 

Also you need to edit the file:  \BlogEngine.Core\Post.cs

 

So you have two files to change so far.

 

\Web\HttpModules\UrlRewrite.cs

\BlogEngine.Core\Post.cs

 

This is for BE 2.0  might be more for BE 2.5?

 

When I get time I will look into it myself. 

 

I would like this feature for myself too :)

 

I personally don't like the [post/page] or the [Date] in the url.

 

it should be    www.mainsite.com/nameofPageorPost.aspx

 

Also you might want to look in to this post:

 

Solved: Extensionless URLs, Permalink redirection, BE2.5

http://blogengine.codeplex.com/discussions/263005


Aug 16, 2011 at 6:17 PM

thanks.... I'll check it out.

Jason

Aug 16, 2011 at 8:17 PM

Thanks Brian.

Yes, this post will help you to change the URL for BE2.0 only.

http://www.blogenginewall.com/post/2011/04/09/Post-URL-Rewrite-For-BlogEngineNET-v20-In-Two-Easy-Steps.aspx

Read following discussion to implement the same in BE2.5

http://blogengine.codeplex.com/discussions/263005

Thanks, BlogEngineWall

Aug 16, 2011 at 9:44 PM

http://blogengine.codeplex.com/discussions/263005

 

Is for extension less urls   taking the ".aspx"  off of the files.

 

It still has the /post in the url

http://www.sexyselect.net/blog/post/2011/06/28/solved-blog-engine-25-extensionless-urls.abcd

http://www.sexyselect.net/blog/post/2011/06/28/solved-blog-engine-25-extensionless-urls.aspx

http://www.sexyselect.net/blog/post/2011/06/28/solved-blog-engine-25-extensionless-urls

Would like it to be in this format:

http://www.rootsite.com/PageorPostName

 

http://www.sexyselect.net/ solved-blog-engine-25-extensionless-urls

 

Like to have it the way you had it for BE 2.0 :)

 

http://www.blogenginewall.com/post/2011/04/09/Post-URL-Rewrite-For-BlogEngineNET-v20-In-Two-Easy-Steps.aspx

 

Or am I missing something?

 

All I see is the methods for Extension less but not folder less URLS.

 

Folder less URLs is what I want :)  

 

No /post

No /page

folders

 

I got it to semi work with your BE 2.0 methods but it was not including the root  so I was getting

www.pagename.aspx

 

So it didn't work.

 

Thanks,

 

Brian Davis

Apr 10, 2012 at 5:21 AM

how can I access post class and do some modifications? can any one suggest me on the same?

 

thanks in advance.