How to prevent a subdomain from redirecting to the main domain when clicking on any page in the subdomain in WordPress?

I would like to describe it specifically like this:

I have a domain example.com managed with Cloudflare and a website managed with CyberPanel.

I created a domain alias sub.example.com in CyberPanel and created a DNS record type A, name = sub, content = IP of the host. When I access sub.example.com/path, it displays the same content as the original domain and the URL displayed is the subdomain, but if I click on the home page or another article, it automatically redirects to the original domain.

For example:

Current link: sub.example.com/path1

When I click on another page whose path is path2, it redirects to example.com/path2, not sub.example.com/path2. I wonder if I need to set anything in the .htaccess file to keep the subdomain active on all pages of the website.

The behavior you’re describing, where your subdomain (sub.example.com ) initially loads content correctly but redirects to the main domain (example.com ) on subsequent page clicks, is likely due to how your CMS or framework is handling URLs. Here’s how to address it:

Understanding the Issue:

  • Your CMS or framework (e.g., WordPress, Joomla) might be generating absolute URLs (starting with http://example.com ) based on your main domain settings, even though you’re accessing the subdomain.
  • This mismatch between the accessed subdomain URL and the generated absolute URL in the content leads to redirects back to the main domain.

Potential Fixes:

1. Base URL Configuration:

  • Access your CMS or framework’s settings and search for options related to “Site URL,” “Base URL,” or “Domain Name.”
  • Change these settings to reflect the subdomain URL (e.g., http://sub.example.com ).
  • Some CMS platforms allow configuring specific base URLs for different subdomains. Explore these options if available.

2. Theme/Plugin Configuration:

  • Certain themes or plugins might be generating absolute URLs. Check their configuration options for settings related to base URLs.
  • If you’re unsure where to look, consider searching your CMS’s documentation or community forums for specific guidance on managing base URLs.

3. .htaccess Rewrites (Advanced)

  • If you’re comfortable with editing configuration files, you can potentially use a rewrite rule in your subdomain’s .htaccess file to rewrite absolute URLs generated by your CMS to include the subdomain.
  • However, this approach requires a good understanding of regular expressions and .htaccess syntax. Be cautious not to introduce errors that might break your website.

4. Contact CMS Support:

  • If the above solutions don’t work or you need further assistance, consider reaching out to your CMS’s support channels. They may be able to offer more specific advice based on your platform.

Additional Tips:

  • After making changes, clear your browser cache and hard reload the page (Ctrl+Shift+R) to ensure the new configuration takes effect.
  • Make backups of your CMS configuration files or themes before editing them, in case you need to revert to a previous state.

By implementing one or a combination of these approaches, you should be able to ensure that your subdomain URL (sub.example.com ) persists across all pages of your website.