Masking a URL for a subdomain in WordPress can be done to make your URLs more user-friendly or to hide the subdomain for branding purposes.
Mask URL for Subdomain in WordPress
Here’s a detailed guide on how to Mask URLs for subdomains using different methods.
1. Using .htaccess for URL Rewriting
The .htaccess file is a powerful configuration file used by the Apache webserver to control various aspects of web traffic. You can use it to rewrite URLs.
Steps:
-
Access Your .hatches File:
Use an FTP client or your hosting control panel’s File Manager to locate and open the .htaccess file in the root directory of your subdomain.
-
Add Rewrite Rules:
Add the following code to your .htaccess file to rewrite the subdomain URLs:
Rewrite Engine On
Rewrite Cond %{HTTP_HOST} ^subdomain\.yourdomain\.com$ [NC]
Rewrite Rule ^(.*)$ http://yourdomain.com/$1 [L,R=301]
Replace subdomain.yourdomain.com with your actual subdomain and yourdomain.com with your main domain.
Save and Upload:
Save the .hatches file and upload it back to your server.
This code will redirect all traffic from subdomain.yourdomain.com to yourdomain.com, effectively masking the subdomain URL.
Also Read: Where Is The DNS Configuration On WordPress?
2. Using a WordPress Plugin
Several plugins can help with URL masking and redirection. One such plugin is Redirection.
Steps:
-
Install and Activate the Plugin:
- Go to your WordPress dashboard, navigate to Plugins > Add New, and search for “Redirection”.
- Install and activate the Redirection plugin.
-
Configure Redirection:
- After activation, go to Tools > Redirection.
- Add a new redirection rule where the source URL is the subdomain path, and the target URL is the desired masked URL.
For example:
- Source URL: /subpath/
- Target URL: http://yourdomain.com/newpath/
This setup will redirect traffic from the subdomain path to the main domain path, masking the URL.
Also Read: Best Tools That Automatically Rewrite Text Using Artificial Intelligence
3. Using PHP for Dynamic URL Masking
If you prefer a programmatic approach, you can use PHP to dynamically mask URLs.
Steps:
Edit Your Theme’s functions’ File:
- Go to your WordPress dashboard, navigate to Appearance > Theme Editor, and select the functions.php file of your active theme.
Add PHP Code:
Add the following code to dynamically redirect and mask the URL:
php
function custom redirect() { if (strops($_SERVER['HTTP_HOST'], 'subdomain.yourdomain.com') !== false) { wp_redirect('http://yourdomain.com' . $_SERVER['REQUEST_URI'], 301); exit(); } } add action('template redirect', 'custom redirect');
Replace subdomain.yourdomain.com with your actual subdomain and http://yourdomain.com with your main domain.
This code will check if the request is coming from the subdomain and redirect it to the main domain while preserving the path and query string.
Masking URLs for subdomains in WordPress can be achieved using .htaccess for URL rewriting, a WordPress plugin like Redirection, or PHP code in your theme’s functions.php file. Choose the method that best fits your technical comfort level and specific needs.
Need expert help with WordPress development? Contact Wacom Designs for professional assistance and support.
Interesting Read:
How To Optimize Website URLs For Better SEO Performance
Subdomain vs Subdirectory: What to Choose?
Troubleshooting a Critical Error on Your WordPress Website: A Comprehensive Guide