After moving your website, you can rewrite the URLs to forward visitors to the new domain name, saving you from traffic and potential sales losses.
You can also rewrite ugly and unwieldy URLs (like www.mystore.com/p=72829) to make them intuitive for shoppers, for example, to something like www.mystore.com/computers/laptops/hp.
The link is easier on the eyes, looks clean, and visitors can extract semantic meanings.
Of course, enabling the mod_rewrite module makes this possible. So let’s learn what it’s, how it works and how to enable it for your account.
What’s Mod_Rewrite?
The mod_rewrite is an Apache module that helps users manipulate or rewrite URLs from the server, allowing them to offer different URLs for the same file.
The module uses a rule-based rewriting engine to let users rewrite URLs.
Users can enable the .htaccess file after activating the mod_rewriting module to let them rewrite URLs via the file on their account control panel instead of on their remote servers.
The .htaccess file allows people to configure server settings without accessing its configuration files.
How the Rewrite Rule Works
The mod_rewrite module relies on rules to match URLs.
When a visitor makes a URL request, the module checks the incoming URLs against existing rules, which use regular expressions to define the match condition or pattern.
If the URL matches a rule, it checks the defined condition and proceeds to the next rule until there are no more rules left, and then executes the substitution.
For instance, if you set up a rewrite rule that forwards users from www to non-www when they make a page request, the module will compare the URL against all existing rules>
If it matches the www to non-www rewrite rule and other defined rules (for instance, HTTP to HTTPS redirect), it executes all the rewrites at once.
The mod_write syntax follows this pattern RewriteRule Pattern Substitution [Flags]:
- RewriteRule specifies the name of the mod_rewrite directive you want to use.
- Pattern shows the regular expression that matches the requested URL.
- Substitution defines the URL of the page you wish to forward to or display.
- Flags specify additional but optional parameters.
Here’s a sample mod_rewrite rule syntax for redirecting non-www URLs to www.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Importance of Mod_Rewrite
Mod_write’s flexibility makes it so powerful and exciting; let’s examine some of its benefits quickly:
- The module helps users to rewrite ugly URLs into friendlier and clean URLs.
- It allows users to redirect visitors from www to non-www URLs or vice versa, which helps address duplicate content issues.
- You can redirect web visitors to your new domain name when you change the domain using a mod_rewrite rule.
- You can block specific or multiple IP addresses using a rewrite rule.
- Most international eCommerce uses mod_rewrite rules to forward visitors from certain countries to the right place, category or store.
- You can deny users access to certain file types, like .pdf, .css, and others.
- It helps web admins force their websites to load securely using HTTPS.
Enabling Mod_Rewrite Module
Scala Hosting installs and enables mod-rewrite modules on Apache web servers by default. If the module is not enabled for you, contact our support, and we’ll get it done.
But if you wish to enable it yourself, then head to your server and find the global configuration file, httpd.conf.
Locate the section that loads a bunch of modules and find the line below within the file.
LoadModule rewrite_module modules/mod\_rewrite.so
Uncomment the line if it’s commented out by removing the hashtag (#) at the beginning of the line. Please ensure to make a backup of your file before modifying the global configuration file.
Need Support?
Scala Hosting installs and enables Apache mod_rewrite on your server by default, so you don’t have to worry about modifying your server global configuration file.
But if you have questions relating to mod_rewrite, you can reach out to our support, and we’ll be glad to answer them.