Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
VGSources
Blog

How to Enable mod_rewrite for Apache on Ubuntu (and Make .htaccess Rules Work)

Run a2enmod rewrite, restart Apache, verify rewrite_module, then configure the correct Directory block so .htaccess RewriteRule directives are actually read.
Length13 min Posted Quest giverVGSources Team
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Enable Apache’s rewrite module with sudo a2enmod rewrite, restart Apache, and confirm that rewrite_module appears in the loaded-module list. If your rules live in .htaccess, that is only half the job: Apache must also allow rewrite directives in the <Directory> block for the site’s real document root. With AllowOverride None, Apache ignores .htaccess files even when mod_rewrite is loaded.

What enabling mod_rewrite actually changes

mod_rewrite is Apache’s runtime URL-rewriting engine. It can redirect a request, map one URL to another resource, apply conditions, and perform pattern-based routing. Ubuntu’s Debian-style Apache layout uses the a2enmod utility to enable modules. The utility enables a named module in the Apache configuration by creating the appropriate links under /etc/apache2/mods-enabled.

As an Amazon Associate I earn from qualifying purchases.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

There are two separate permissions involved when you use rewrite rules:

  • The module must be loaded. This is what a2enmod rewrite changes.
  • The configuration context must permit the directives. For a rule in .htaccess, the matching <Directory> block must allow the required override category. A loaded module does not override AllowOverride None.

Keeping those conditions separate explains the common symptom “the module is enabled, but my .htaccess rule does nothing.”

Before you change Apache

Confirm that Apache2 is the server you are configuring

The commands in this guide target Ubuntu’s Apache2 service, named apache2.service. Confirm that Apache2 is installed and that you are working on the instance serving the site. If the machine has more than one web server or more than one Apache installation, a successful command against the wrong instance will not change the server receiving your requests.

You need an account that can run commands with sudo, because enabling a module changes files in Apache’s system configuration. The procedure does not require a separate product, plugin, or paid service; it uses the Apache and Ubuntu configuration already on the server.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Find the active site and document root

Locate the enabled virtual-host configuration and read its DocumentRoot. Ubuntu commonly stores enabled site links in /etc/apache2/sites-enabled, with the corresponding site configuration in /etc/apache2/sites-available. The directory in your <Directory> block must be the actual directory serving the application. Do not change the filesystem root or an unrelated site’s directory just because it is easy to find.

For a default installation, the document root is often /var/www/html, but use the path declared by the active virtual host when it differs. If several hostnames are configured, identify the host that answers the hostname you are testing. A rule in one site’s directory cannot authorize a file in another site’s directory.

Enable and verify the module

  1. Enable rewrite. Run:
    sudo a2enmod rewrite

    a2enmod is Ubuntu’s module-management script. It enables the named module in the Apache configuration, normally by creating links in /etc/apache2/mods-enabled. If Ubuntu reports that the module is already enabled, do not treat that message as a failure. Continue with the restart and verification steps, then investigate the active configuration if the rule still fails.

  2. Restart Apache. Load the changed module configuration:
    sudo systemctl restart apache2.service

    A restart is required after changing enabled modules. A configuration reload is not a substitute in this procedure unless you have separately verified that the new module state has been loaded. If the restart reports an error, resolve that Apache configuration problem before testing rewrite behavior.

    Free tools Windows power users keep installed

    One-click scans. No signup required.

    Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  3. Check the loaded module list.
    apache2ctl -M | grep rewrite

    The expected output contains rewrite_module. No matching line means the Apache instance queried by apache2ctl has not loaded the module.

These commands establish only that the rewrite engine is available. They do not authorize per-directory directives or prove that a particular virtual host is using your rule. Treat module loading and rule execution as two checkpoints: first verify the module, then verify the directory or virtual-host context.

Allow rewrite directives in .htaccess

Use the narrowest override policy that works

Apache’s default AllowOverride policy is None. When both AllowOverride None and AllowOverrideList None apply, .htaccess files are completely ignored. Put the override policy in the <Directory> block for the document root or application directory that actually contains the file.

For ordinary rewrite directives, AllowOverride FileInfo is the focused choice. It permits directives such as RewriteEngine, RewriteBase, RewriteCond, RewriteOptions, and RewriteRule without granting every possible .htaccess override.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<Directory /var/www/html>
    AllowOverride FileInfo
    Require all granted
</Directory>

Replace /var/www/html with the active site’s real directory. After editing the server configuration, restart Apache and test the site again. If the file is in a subdirectory, verify that the directory hierarchy leading to that file is covered by a suitable policy.

When AllowOverride All is appropriate

AllowOverride All enables a much broader set of directives. Some applications require that broader compatibility, but it gives every permitted .htaccess file more control than a rewrite-only setup. Use it only when the application genuinely needs those additional overrides. Apache recommends leaving the filesystem root at AllowOverride None for security and performance, then changing only the directory that hosts the site.

Check the full path, not just the filename

Apache searches for .htaccess in the directory hierarchy leading to the requested resource. A file in a parent directory can be affected by a different <Directory> policy than a file in the application’s document root. Check the matching directory blocks, file ownership and permissions, and the error log if Apache returns HTTP 500 or silently ignores the rule.

Do not assume that seeing a .htaccess file on disk proves Apache reads it. The active virtual host, the requested path, and every applicable directory policy determine whether the file is processed. This is why changing a similarly named directory block often has no visible effect.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Create a minimal .htaccess rule

Once the module is loaded and FileInfo overrides are permitted, place this minimal file in the site’s document root:

RewriteEngine On
RewriteRule ^old-path/?$ /new-path [R=301,L]

What each line does

  • RewriteEngine On enables the runtime rewriting engine in this configuration context. It is required where the rules run.
  • ^old-path/?$ matches the relative path old-path, with an optional trailing slash.
  • /new-path is the destination sent to the client.
  • [R=301,L] makes the result a permanent redirect and stops processing later rules for that pass.

In per-directory context, the RewriteRule pattern does not begin with a slash. Therefore use ^old-path, not ^/old-path. The leading slash belongs in the substitution when you want an absolute URL path such as /new-path.

Per-directory rewriting also requires at least one of FollowSymLinks or SymLinksIfOwnerMatch to be enabled. If a correct-looking rule still produces an error, check the applicable directory options as well as the override policy.

Start with one rule and one test URL. Once the simple match works, add additional conditions or routing rules one at a time. A minimal rule makes it easier to distinguish a module problem from a pattern, directory, or permission problem.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Put rules in a virtual host instead of .htaccess

You can place rewrite directives in the virtual-host or server configuration rather than delegating them to .htaccess. This centralizes control and avoids a directory-by-directory lookup on requests, but it requires access to Apache’s server configuration and a restart after edits.

Include RewriteEngine On in every virtual host that needs rewriting. Apache’s rewrite configuration is not inherited by virtual hosts: enabling it in one site does not turn it on for another site on the same server.

This distinction matters when a rule works for one hostname but not another. Check the configuration for each enabled virtual host, its document root, and the context in which the rule is declared. A centralized rule can be easier to audit, while .htaccess is useful when application teams need per-directory control without editing the main server configuration.

Choose the right configuration style

Choice Best for Trade-off
.htaccess Applications or teams that need per-directory delegation without editing the main Apache configuration Depends on AllowOverride, is distributed across directories, and can be harder to audit centrally
Virtual-host/server configuration Administrators who control Apache and want centralized rules and easier performance analysis Requires server-level access and a restart or configuration deployment after changes
AllowOverride FileInfo Applications needing rewrite directives in .htaccess Least-privilege override category for the rewrite directives listed by Apache
AllowOverride All Applications that demonstrably need many kinds of .htaccess directives Broader authority than rewriting alone; avoid it when FileInfo is sufficient

Choose the context before copying rules between servers. A pattern written for server or virtual-host context may need adjustment in per-directory context, where Apache removes the directory prefix and does not provide a leading slash to the rule pattern.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When a simple Redirect is better than mod_rewrite

Not every redirect needs a regular expression or a condition. For a straightforward, unconditional path change, Apache’s Redirect or RedirectMatch directives are simpler and more efficient than a rewrite rule. Use mod_rewrite when you need conditional logic, complex patterns, environment-dependent routing, or transformations that those directives cannot express.

Apache documents this distinction in its remapping guidance: https://httpd.apache.org/docs/2.4/rewrite/remapping.html. The choice is about the requirement, not about enabling more features than the redirect needs. A static redirect should remain static when that fully describes the behavior you want.

Troubleshoot a rule that does nothing

“a2enmod says rewrite is already enabled”

That message means Ubuntu already has the module enabled in its module layout. Restart Apache anyway, then run apache2ctl -M | grep rewrite. If the expected rewrite_module line is absent, confirm that apache2ctl is querying the intended Apache installation and inspect /etc/apache2/mods-enabled for the rewrite-related links.

The module list has no rewrite_module entry

First check that the enable command was run successfully and that the links under /etc/apache2/mods-enabled exist. Then restart apache2.service and repeat the module check. If multiple Apache instances or installations exist, compare the service being restarted with the binary used by apache2ctl.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

.htaccess is ignored

Inspect the <Directory> block matching the requested document root. AllowOverride None disables .htaccess processing; AllowOverrideList None can also prevent directives from being accepted. Set AllowOverride FileInfo for rewrite directives, or use AllowOverride All only when broader application compatibility is required. Verify that the file is in the directory hierarchy Apache serves and that the account running Apache can read it.

Apache returns HTTP 500 after adding the file

A 500 response commonly indicates an invalid directive, an override category that does not permit the directive, an unavailable option required for per-directory rewriting, or a syntax problem in the rule. Check the Apache error log immediately after reproducing the request. The log identifies the file and directive Apache rejected; correct that issue before testing the redirect again.

The pattern looks right but never matches

Check the context. In .htaccess, the per-directory path supplied to RewriteRule omits the leading slash. A pattern written for server context may therefore fail when copied unchanged into a per-directory file. Start with the minimal ^old-path/?$ example, confirm the request path, and add conditions only after the basic match works.

The rule works on one site but not another

Inspect each virtual host independently. Rewrite settings are not inherited by virtual hosts, and each site may point to a different document root or apply a different AllowOverride policy. Put RewriteEngine On and the rules in the relevant virtual host or ensure that the relevant directory permits the site’s .htaccess.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A redirect appears cached

The example uses status code 301, which tells clients and intermediaries that the redirect is permanent. During initial testing, use a temporary test path or a temporary redirect strategy appropriate to your deployment so that an earlier permanent response does not obscure later rule changes. Once the destination and matching behavior are confirmed, apply the permanent redirect deliberately.

A repeatable verification checklist

  1. Identify the virtual host handling the hostname and record its actual DocumentRoot.
  2. Run sudo a2enmod rewrite and note whether Ubuntu enables the module or reports it is already enabled.
  3. Run sudo systemctl restart apache2.service.
  4. Confirm rewrite_module with apache2ctl -M | grep rewrite.
  5. If using .htaccess, set AllowOverride FileInfo in the matching <Directory> block and leave unrelated directories at their least-privilege setting.
  6. Ensure RewriteEngine On appears in the context where the rule executes.
  7. Use a per-directory pattern without a leading slash.
  8. Request the old path, inspect the HTTP result and Location header, and then check the error log if the response is unexpected.

Configuration mistakes to avoid

  • Enabling the module but never restarting Apache: the changed module state is not loaded until the service restarts.
  • Changing the wrong directory block: a permissive policy on an unrelated path does not authorize the site’s actual .htaccess.
  • Using AllowOverride All by default: grant only the override category the application needs.
  • Adding a slash to a per-directory pattern: RewriteRule ^/old-path does not match the same way as RewriteRule ^old-path in .htaccess.
  • Assuming virtual-host settings carry over: each relevant virtual host needs its own rewrite configuration.
  • Using rewrite for a static redirect: prefer Redirect or RedirectMatch when no conditions or complex pattern processing are required.
  • Testing only the browser address bar: verify the actual HTTP response and Location header so a cached 301 does not hide the current server behavior.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Working example from start to finish

Suppose the active site serves /var/www/html and you want /old-path to send visitors to /new-path.

  1. Enable the module with sudo a2enmod rewrite.
  2. Restart Apache with sudo systemctl restart apache2.service.
  3. Verify that apache2ctl -M | grep rewrite prints rewrite_module.
  4. In the active site’s matching directory configuration, use:
    <Directory /var/www/html>
        AllowOverride FileInfo
        Require all granted
    </Directory>
  5. Create /var/www/html/.htaccess containing:
    RewriteEngine On
    RewriteRule ^old-path/?$ /new-path [R=301,L]
  6. Restart Apache after the server-configuration change, request /old-path, and confirm that Apache returns a permanent redirect to /new-path.

If this exact example fails, do not add more rules first. Re-check the active virtual host, the matching directory path, the override policy, the module list, the per-directory pattern, and the error log in that order.

Frequently asked questions

Do I need to install a separate rewrite package?

No separate product is required for this procedure. Ubuntu’s Apache2 layout includes the a2enmod utility used to enable the module. You need Apache2 installed and sufficient privileges to change its configuration.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why does enabling the module not make my .htaccess rule work?

Module loading and .htaccess authorization are independent. The matching <Directory> block may still have AllowOverride None or an AllowOverrideList that excludes the rewrite directives. Set AllowOverride FileInfo in the actual application directory, then restart and test again.

What should the verification command print?

apache2ctl -M | grep rewrite should include rewrite_module. If it does not, inspect the links in /etc/apache2/mods-enabled, confirm the intended Apache installation, and restart apache2.service.

Should I use AllowOverride FileInfo or AllowOverride All?

Use FileInfo when rewrite directives are all the application needs. Use All only when the application demonstrably requires other kinds of .htaccess overrides. Keeping the narrower setting reduces unnecessary delegated control.

Why is my RewriteRule not matching /old-path?

In .htaccess, Apache supplies a per-directory path without a leading slash. Write ^old-path, not ^/old-path. Also confirm that RewriteEngine On appears in that same context.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Do rewrite settings apply to every virtual host?

No. Apache does not inherit rewrite configurations between virtual hosts. Add RewriteEngine On and the needed rules to each relevant host, or authorize the correct directory for each site’s .htaccess.

When should I avoid mod_rewrite?

For a simple, unconditional redirect, use Redirect or RedirectMatch when those directives meet the requirement. They are simpler and more efficient than introducing a rewrite rule for a job that does not need conditions or complex pattern processing.

What is the first place to look after an HTTP 500?

Read Apache’s error log immediately after reproducing the request. Then check the directive’s override category, the applicable directory options, the file permissions, and the rule syntax. The error entry usually identifies the rejected file or directive.

Frequently Asked Questions

How do I enable mod_rewrite on Apache Ubuntu?

Run sudo a2enmod rewrite, restart with sudo systemctl restart apache2.service, and verify with apache2ctl -M | grep rewrite. The output should contain rewrite_module.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why does my .htaccess RewriteRule do nothing?

Confirm that the matching <Directory> block allows AllowOverride FileInfo. With AllowOverride None, Apache ignores .htaccess even when the module is loaded. Also check the document root, permissions, and the per-directory pattern.

Does AllowOverride All have to be enabled?

No. AllowOverride FileInfo is sufficient for the listed rewrite directives in most setups. Use AllowOverride All only when the application needs broader .htaccess control.

Why does a rule work in one virtual host but not another?

Rewrite configuration is not inherited by virtual hosts. Check each host’s DocumentRoot, RewriteEngine On setting, and directory override policy independently.

Should a .htaccess RewriteRule start with a slash?

No. In per-directory context, the pattern omits the leading slash, so use ^old-path. The substitution can include a leading slash, as in /new-path.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The Bottom Line

Enable the module with sudo a2enmod rewrite, restart apache2.service, and verify rewrite_module. Then authorize rewrite directives in the real site’s <Directory> block—normally with AllowOverride FileInfo—and remember that .htaccess patterns omit the leading slash.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

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

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More quests from Patch Notes

  1. How to Set Up a RedM (RDR2) Server in 2026: License Key, txAdmin and server.cfg, Step by StepBlog11min
  2. Best RedM (RDR2) Server Hosting in 2026: Comparing Four Hosts on Slots, Memory and PriceBlog10min
  3. How to Host a Mindustry Server in 2026: server-release.jar, Port 6567 and the Commands That MatterBlog7min
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.