Qwik Host Logo

How to Host a Blog on a /blog Sub-folder in an ASP.NET Project

Here’s a detailed guide on how to host a qwik.host blog on a /blog sub-folder in an ASP.NET project. This will demonstrate using subdomain.myqwik.blog as the source blog and rewriting it to appear under www.mydomain.com/blog.

Dec 16, 2024

How to Host a  Blog on a /blog Sub-folder in an ASP.NET Project

If you have a blog hosted at subdomain.myqwik.blog and you want it to appear under a subdirectory like www.mydomain.com/blog in your ASP.NET application, you can achieve this with URL Rewriting.

We’ll cover different scenarios depending on how your ASP.NET project is hosted:

1. Using Nginx (Linux Reverse Proxy)

2. Using IIS with URL Rewrite Module

3. Programmatic Solution with ASP.NET Core Middleware

1. Using Nginx as a Reverse Proxy (For Linux Hosting)

If your ASP.NET Core app runs on a Linux server (e.g., Ubuntu), and you are using Nginx as a reverse proxy, you can set up rewrite rules to serve the blog under /blog.

Step-by-Step Configuration:

1. Access your Nginx Configuration File

Open your Nginx configuration file for your domain. For example:

sudo nano /etc/nginx/sites-available/mydomain.com

2. Add the Reverse Proxy Rules

Add the following configuration inside the server block:

server {
    listen 80;
    server_name www.mydomain.com;

    # Reverse proxy for /blog
    location /blog {
        proxy_pass https://subdomain.myqwik.blog/blog;
        proxy_set_header Host subdomain.myqwik.blog;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location / {
        proxy_pass http://localhost:5000; # Your ASP.NET Core application
    }
}

• proxy_pass: Passes requests for /blog to https://subdomain.myqwik.blog/blog.

• proxy_set_header Host: Ensures the host header is forwarded correctly.

3. Restart Nginx:

Restart Nginx to apply the changes:

sudo systemctl restart nginx

What Happens:

Requests to www.mydomain.com/blog are transparently proxied to subdomain.myqwik.blog/blog.

2. Using IIS with URL Rewrite Module (Windows Hosting)

If your ASP.NET project is hosted on a Windows server using IIS, you can use the URL Rewrite Module to achieve the same result.

Step-by-Step Configuration:

1. Install the URL Rewrite Module

• Download and install the IIS URL Rewrite Module if it’s not already installed:

Download URL Rewrite Module.

2. Edit Your web.config File

Open your ASP.NET project’s web.config file and add the following rewrite rules:

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <!-- Redirect /blog to subdomain.myqwik.blog/blog -->
        <rule name="Rewrite Blog Root" stopProcessing="true">
          <match url="^blog$" />
          <action type="Rewrite" url="https://subdomain.myqwik.blog/blog" />
        </rule>

        <!-- Redirect /blog/* to subdomain.myqwik.blog/blog/* -->
        <rule name="Rewrite Blog Subpaths" stopProcessing="true">
          <match url="^blog/(.*)" />
          <action type="Rewrite" url="https://subdomain.myqwik.blog/blog/{R:1}" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

• The first rule rewrites requests to /blog.

• The second rule rewrites all subpaths under /blog to the corresponding paths on subdomain.myqwik.blog.

3. Restart IIS:

Use the IIS Manager or run the following command in the terminal:

iisreset

3. Programmatic Solution Using ASP.NET Core Middleware

If you prefer handling this directly in your ASP.NET Core project, you can use the URL Rewrite Middleware.

Step-by-Step Configuration:

1. Install Required Package:

If you don’t already have the Microsoft.AspNetCore.Rewrite package, install it using NuGet:

dotnet add package Microsoft.AspNetCore.Rewrite

2. Add Rewrite Rules in Startup.cs or Program.cs:

Modify your application’s pipeline to include URL rewriting:

using Microsoft.AspNetCore.Rewrite;

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    var rewriteOptions = new RewriteOptions()
        .AddRewrite("^blog$", "https://subdomain.myqwik.blog/blog", skipRemainingRules: true)
        .AddRewrite("^blog/(.*)", "https://subdomain.myqwik.blog/blog/$1", skipRemainingRules: true);

    app.UseRewriter(rewriteOptions);

    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
        endpoints.MapRazorPages();
    });
}

• AddRewrite: Adds a rewrite rule where requests to /blog and /blog/* are redirected to https://subdomain.myqwik.blog.

3. Run Your Application:

Start your application, and the middleware will handle rewriting /blog paths.

Testing Your Setup

Once you’ve configured the rewrite rules:

1. Go to www.mydomain.com/blog.

2. Verify that content from subdomain.myqwik.blog/blog is displayed.

3. Test subpaths like www.mydomain.com/blog/sample-post to ensure they work correctly.

Which Method Should You Choose?

• Use Nginx if your ASP.NET Core project is hosted on a Linux server. It’s lightweight and highly performant for reverse proxying.

• Use IIS with web.config if your project is hosted on a Windows Server.

• Use ASP.NET Core Middleware if you want to handle everything programmatically within your application.

By following these steps, you can successfully host your qwik.host blog under a /blog subdirectory while keeping the blog content at subdomain.myqwik.blog. Let me know if you encounter any issues or need further clarification! 🚀

Published with

logo