Rewrite rules with htaccess ⚠️

Let’s talk about .htaccess rewrite rules a powerful tool for managing your website’s structure and how it interacts with visitors.

I’ve been using these rules for years and they’ve been a lifesaver in streamlining website management and making things easier for both me and my clients.

Why Rewrite Rules?




Imagine you’re juggling multiple websites on a single cPanel account.

It’s easy to add “Add-On Domains” but the cPanel structure can feel clunky: all domains end up as subfolders within the same root directory.

This can get messy and confusing.

That’s where .htaccess rewrite rules step in to save the day.

Setting Up a Clean Structure

The beauty of .htaccess rewrite rules lies in their ability to create a much cleaner website organization.

You can separate your main domain from add-on domains making it easier to manage content and ensuring a clear logical structure.

Let’s break this down step-by-step.

We’ll focus on a typical cPanel environment.

The Preparation

  1. Fresh Start or Backup: If you’re starting a brand new website fantastic! If you’re working with an existing website make sure you have a full backup. cPanel’s backup assistant is your best friend here.
  2. FTP Access: You’ll need a good FTP client (FileZilla is a popular choice).

Setting Up Your Domains

  1. Sub-folder for Your Domain: Create a subfolder in the /public_html directory. This subfolder should be named after your domain. For example if your domain is example.com create a folder named example.com.
  2. Moving the Files: Move all the files and folders from your main domain into this newly created subfolder. For example move everything inside the admin config data themes and index.php folders into the example.com subfolder. If there’s already a .htaccess file for your main domain move that as well.

The Power of .htaccess

  1. New .htaccess File: Create a new .htaccess file in the /public_html directory (the one at the root of your website).

  2. Rewrite Rules: Paste the following rewrite rules into the new .htaccess file making sure to replace example.com with your actual domain name:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^example.com$ 
    RewriteCond %{HTTP_HOST} ^www.example.com$
    RewriteCond %{REQUEST_URI} !^/example.com
    RewriteRule ^(.*)$ example.com/$1 

    Explanation:

    • RewriteEngine On: This line activates the rewrite engine making the rest of the rules work.
    • RewriteCond: These conditions tell Apache to apply the rewrite rule only if the HTTP_HOST (the website’s address) matches example.com or www.example.com. The !^/example.com ensures that the rule doesn’t apply to files directly within the example.com subfolder.
    • RewriteRule: This line is the core of the rule. It says: “For any request coming in if the conditions are met rewrite the URL by prepending the example.com subfolder before the actual file path.”

Adjusting Your CMS

Now your website should be working correctly but you might encounter a small issue.

Some content management systems (CMS) like Joomla! and WordPress rely on relative URLs.

This means they assume the website’s content is located at the root level.

Because of our rewrite rules your website’s links might now look like https://www.example.com/example.com/index.php instead of https://www.example.com/index.php. To fix this you’ll need to make a simple change in your CMS configuration:

Joomla! Configuration

  1. Open configuration.php: Use an FTP client to access your website’s root folder. Find and open the file named configuration.php. This file contains Joomla!’s configuration settings.

  2. Modify the $live_site Variable: Look for the line containing the $live_site variable and update the URL to reflect your main domain:

    public $live_site = 'https://www.example.com/'; 

    If the configuration.php file is read-only you’ll need to change the file permissions to make it writable first.

You can usually do this with your FTP client.

  1. URL Rewriting in Joomla! (Optional): If you’re using Joomla!’s URL rewriting feature make sure to update the RewriteBase directive in the .htaccess file located within the example.com subfolder. Remove the hash symbol (#) at the beginning of the line and change it to:

    RewriteBase /example.com

WordPress Configuration

  1. Open wp-config.php: Access the WordPress root folder through your FTP client and open the file named wp-config.php.

  2. Modify WP_HOME and WP_SITEURL: Find the lines containing the WP_HOME and WP_SITEURL constants and update the URL to reflect your main domain:

    define('WP_HOME' 'https://example.com');
    define('WP_SITEURL' 'https://example.com');

The End Result

After making these changes save your files and refresh your website.

The changes should be seamless for your visitors.

Behind the scenes your website now has a clean organized structure:

/public_html
   |
   ---.htaccess (rewrite rules for main domain)
   |
   ---example.com (subfolder for example.com)
       |
       ---index.php
       |
       ---admin
       |
       ---config
       |
       ---data
       |
       ---themes
       |
       ---.htaccess (rewrite rules for example.com subfolder if using Joomla!)
       |
       ---... (other files and folders for example.com)

Expanding Your Knowledge

These rewrite rules are just a starting point.

The possibilities for managing website structures are endless.

To delve deeper into the world of .htaccess explore the Apache documentation.

You’ll find a wealth of information about the different rewrite conditions rules and flags that let you craft custom solutions for your website.

Key Takeaways

  • Clean Structure: .htaccess rewrite rules create a cleaner website organization separating main domains from add-on domains for easier management.
  • Easy Implementation: The process is straightforward requiring basic FTP knowledge and a bit of configuration tweaking in your CMS.
  • Powerful Tool: Rewrite rules provide a high level of control over how your website’s URLs are handled giving you the flexibility to customize the user experience.

If you have any more questions feel free to ask.

Happy coding!




Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top