October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk11 min

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

A complete Ubuntu Apache guide: enable mod_rewrite, verify the module, authorize .htaccess with the right AllowOverride policy, add safe RewriteRule examples, and fix common failures.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Enable Apache’s rewrite engine with sudo a2enmod rewrite, restart Apache with sudo systemctl restart apache2.service, and verify that rewrite_module appears in apache2ctl -M. If rules in .htaccess still do nothing, loading the module is not the missing step: the matching <Directory> block must permit the required overrides, normally with AllowOverride FileInfo.

What enabling mod_rewrite actually changes

mod_rewrite is Apache’s runtime URL-rewriting engine. It can redirect a request, internally map one URL to another resource, add conditions, and apply regular-expression rules. On Ubuntu, Apache modules are managed through the Debian/Ubuntu layout. The a2enmod utility enables a named module by creating the appropriate links in /etc/apache2/mods-enabled; enabling the module does not itself restart the web server or authorize every directory to read .htaccess files.

As an Amazon Associate I earn from qualifying purchases.

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.

There are therefore two separate checks:

  • Is the module loaded? The Apache process must load rewrite_module.
  • Is rewriting allowed where your rules live? A .htaccess file is ignored when its directory uses AllowOverride None (and AllowOverrideList None).

Keeping those checks separate makes most “RewriteRule does nothing” problems straightforward to diagnose.

Before changing Apache, identify the site and document root

Make changes to the virtual host that serves the request, not to an unrelated example site. Confirm that Apache2 is installed and running, then inspect the active virtual-host map and configuration files.

  1. Check the service and version:
    systemctl status apache2.service
    apache2ctl -v

    The status output should show the service running. If it is stopped, start it before testing rewrite behavior:

    sudo systemctl start apache2.service
  2. List the virtual hosts Apache selected:
    sudo apache2ctl -S

    The output identifies each listening address, the default virtual host, and the configuration file and line that define each host.

  3. Open the configuration file named by that output. Ubuntu commonly stores enabled site links in /etc/apache2/sites-enabled/, with the corresponding files in /etc/apache2/sites-available/. Find the DocumentRoot for the hostname you are testing. That path is the directory whose <Directory> policy must permit your .htaccess directives.
  4. Check whether the requested path is actually inside that document root. A rule in /var/www/example/.htaccess cannot affect a site whose active document root is /srv/www/other-site.

Do not loosen the filesystem root or every site just to make one application work. Apply the narrowest setting to the directory that serves the application.

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

Enable and verify the module

1. Enable rewrite

Run:

sudo a2enmod rewrite

If Ubuntu reports that the module is already enabled, that is not an error. Continue with the restart and verification steps; the message only means the module links already exist.

2. Restart Apache

Load the changed module configuration by restarting the service:

sudo systemctl restart apache2.service

If the restart fails, do not test the site until the configuration error is fixed. Ask Apache to report the problem:

sudo apache2ctl configtest

A successful check prints Syntax OK. A failed check names the file and line that must be corrected.

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

3. Confirm that Apache loaded it

apache2ctl -M | grep rewrite

The expected listing is:

 rewrite_module (shared)

The exact spacing can vary, but the important text is rewrite_module. If no line appears, the command may be querying a different Apache installation than the service you restarted, or the module link is missing. Inspect the enabled-module directory:

ls -l /etc/apache2/mods-enabled/ | grep rewrite

On a normal Ubuntu installation, you should see links for the rewrite module’s load and configuration files. Re-run sudo a2enmod rewrite, then restart and verify again. If the host runs more than one Apache instance, make sure the service name, binary, and configuration path all refer to the same instance.

Allow .htaccess to read rewrite directives

Loading mod_rewrite does not grant permission to process per-directory configuration. Apache’s default policy is commonly AllowOverride None; with that policy, .htaccess files are completely ignored. Add an appropriate directory block for the actual document root or application directory in the virtual-host/server configuration.

Least-privilege setting for rewrite rules

<Directory /var/www/html>
    AllowOverride FileInfo
    Require all granted
</Directory>

Replace /var/www/html with the real document root. AllowOverride FileInfo permits the rewrite directives normally used in a .htaccess file, including RewriteEngine, RewriteBase, RewriteCond, RewriteOptions, and RewriteRule. Require all granted controls access to the directory; it is separate from rewrite permission and should match the access policy your site needs.

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

When, if ever, to use AllowOverride All

AllowOverride All enables a much broader set of per-directory overrides. Use it only when the application genuinely requires those additional directives. It increases the amount of configuration that can be changed by files beneath that directory, which makes auditing and performance analysis harder. Keep the filesystem root at AllowOverride None and change only the directory that hosts the site.

Apply the change safely

  1. Edit the active virtual-host file or another server-level configuration included by Apache.
  2. Check syntax:
    sudo apache2ctl configtest
  3. Reload when only server configuration changed:
    sudo systemctl reload apache2.service

    A full restart is also valid and is required after enabling a module if you have not already restarted.

  4. Request a URL that exercises the rule and watch the response status and Apache error log while testing.

Create a minimal .htaccess rule

Place the file in the document root (or in the application directory whose <Directory> block permits overrides):

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

This example sends a permanent redirect from /old-path (with an optional trailing slash) to /new-path. Test with a browser or with:

curl -I http://your-host.example/old-path

You should see a 301 status and a Location header pointing to /new-path. Use a temporary R=302 while developing if you do not want browsers to cache a permanent redirect; change it to 301 only when the destination is final.

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

Understand per-directory pattern syntax

In .htaccess (per-directory) context, Apache strips the directory prefix before matching a RewriteRule. The pattern therefore does not begin with a leading slash. Write ^old-path$, not ^/old-path$. A leading slash is appropriate in some server or virtual-host contexts only when the rule’s context requires it; always check the context in which the rule runs.

RewriteEngine On enables the runtime engine in the context containing the rules. Without it, the rules are present but inactive. If rules are placed directly in a virtual-host or server configuration, put RewriteEngine On in each relevant virtual host: rewrite configuration is not inherited by virtual hosts.

Per-directory rewriting and symbolic links

Apache’s per-directory rewriting requires at least one of FollowSymLinks or SymLinksIfOwnerMatch to be enabled for that directory. If a deployment uses symbolic links and rules fail despite correct overrides, inspect the directory’s Options policy rather than adding broader overrides blindly. Change only what the deployment’s ownership and security model permits.

Understand effective directory scope

The <Directory> path must match the filesystem location Apache uses after resolving the virtual host’s DocumentRoot. Enabling overrides for a parent directory does not help if the request is served by a different site, a different document root, or a more specific directory policy. This is why apache2ctl -S and the active DocumentRoot should be checked before editing a .htaccess file.

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

Keep the policy as narrow as the application allows. A site that only needs URL rules normally needs AllowOverride FileInfo, not AllowOverride All. If an application also depends on other per-directory directives, identify those requirements explicitly before expanding the override class. When Apache is configured with AllowOverrideList, make sure the directives used by the file are permitted there as well; a restrictive list can still block a directive even when the module itself is loaded.

There is no separate paid component in this procedure. The commands use Ubuntu’s Apache module layout and Apache configuration directives; the operational cost is limited to applying and testing the configuration on the server.

Choose where rewrite rules should live

Configuration style Best fit Trade-offs
.htaccess Shared hosting, delegated application configuration, or situations where the site owner cannot edit the virtual-host file. Apache checks the file for requests in that directory tree; rules are distributed and depend on AllowOverride permission.
Virtual-host/server configuration Sites you administer centrally, especially high-traffic or multi-site servers. Centralized control and easier performance analysis, but requires access to Apache’s server configuration and a reload/restart after changes.

For a centrally managed site, server-level rules avoid delegating configuration through writable application directories. For an application that expects to ship its own .htaccess, grant only the override class it needs and keep the rest of Apache locked down.

Rules in server configuration also make the owning virtual host unambiguous. Because rewrite settings are not inherited by virtual hosts, repeat the relevant RewriteEngine On and rules in every host that needs them. Conversely, do not assume that a rule tested on the default site will apply to a named host simply because both hosts use Apache’s same process.

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

When a simpler redirect is better than mod_rewrite

Not every redirect needs a regular-expression engine. For a straightforward, unconditional URL move, Apache’s Redirect or RedirectMatch directives are simpler and more efficient than a rewrite rule. Apache documents these alternatives in its URL remapping documentation.

Redirect 301 /old-path /new-path

Use mod_rewrite when you need conditions, captures, environment-based decisions, host or query-string logic, or an internal mapping rather than a client-visible redirect. Choosing the simpler directive reduces the number of moving parts and makes the intended behavior easier to audit.

Troubleshoot rules that are ignored or return HTTP 500

The module is reported as already enabled

Run the restart and verification commands anyway. “Already enabled” refers to the module links, not to whether the currently running process has loaded them. Confirm rewrite_module with apache2ctl -M and make sure that command addresses the same Apache instance as apache2.service.

apache2ctl -M shows no rewrite_module

  • Run sudo a2enmod rewrite again and read its output for errors.
  • Inspect /etc/apache2/mods-enabled for the rewrite symlinks.
  • Restart apache2.service, then repeat the module check.
  • If multiple installations or custom prefixes exist, compare the binary and configuration used by the service with the one invoked by apache2ctl.

The .htaccess file is ignored

Check the directory block that matches the real document root. AllowOverride None disables processing; AllowOverrideList None can also prevent directives from being used. Set AllowOverride FileInfo for the relevant directory, validate syntax, and reload Apache. Confirm the file is named exactly .htaccess, is located beneath that directory, and is readable by the Apache process.

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

Every request returns HTTP 500 after adding the file

A syntax or directive error is usually recorded in Apache’s error log. Inspect the log immediately after reproducing the failure, then run:

sudo apache2ctl configtest

Common causes include a misspelled directive, a directive not permitted by the selected override class, malformed regular-expression syntax, or a rule that creates a redirect loop. Temporarily reduce the file to the two-line minimal example, verify that it works, and add conditions and flags one at a time.

The rule is syntactically valid but never matches

  • Remove the leading slash from a per-directory pattern: use ^old-path/?$, not ^/old-path/?$.
  • Check whether the request includes a directory prefix that is stripped before matching.
  • Confirm that RewriteEngine On appears in the same effective context as the rule.
  • Check the requested hostname and virtual host. A rule in one site is not inherited by another virtual host.
  • Test the exact path, including or excluding a trailing slash and any query string your conditions inspect.

The rule works on one site but not another

Inspect each virtual host separately. Rewrite settings are not inherited between virtual hosts, and each site can point to a different document root and <Directory> policy. Copying a working .htaccess file is not enough if the second site’s directory block still uses AllowOverride None.

Redirects appear cached or loop

A browser may cache a 301. Test with curl -I or a private browser window while developing. Follow the Location headers and verify that the destination does not match the source rule again. Use a temporary 302 during iteration, then switch to 301 only after confirming the final mapping.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

A repeatable enable-and-test checklist

  1. Identify the hostname, active virtual-host file, and document root with sudo apache2ctl -S.
  2. Enable the module: sudo a2enmod rewrite.
  3. Restart Apache: sudo systemctl restart apache2.service.
  4. Verify loading: apache2ctl -M | grep rewrite should show rewrite_module.
  5. For .htaccess, set AllowOverride FileInfo in the matching <Directory> block and retain Require all granted or your site’s intended access rule.
  6. Run sudo apache2ctl configtest before every reload or restart.
  7. Add RewriteEngine On and one minimal rule; remember that per-directory patterns omit the leading slash.
  8. Test with curl -I, inspect the status and Location header, and read the error log if the result is unexpected.
  9. Once the behavior is correct, replace temporary redirects with the permanent status you actually intend and document the rule’s purpose.

How to roll back safely

If enabling the module causes an unexpected interaction, disable it with:

sudo a2dismod rewrite

Then validate and restart:

sudo apache2ctl configtest
sudo systemctl restart apache2.service

Disabling the module will make rewrite directives unavailable; remove or disable dependent rules first if they prevent Apache from starting. For a bad site-level change, restore the previous virtual-host or .htaccess version, run the syntax check, and reload. Keeping changes small—module, directory policy, then one rule—makes the failing layer identifiable and the rollback quick.

Frequently Asked Questions

Do I need to restart Apache after running a2enmod rewrite?

Yes. a2enmod creates the enabled-module links, but the running Apache process must be restarted with sudo systemctl restart apache2.service before it loads the change.

Why does apache2ctl -M not show rewrite_module after enabling it?

Inspect /etc/apache2/mods-enabled, restart Apache, and confirm that apache2ctl and apache2.service refer to the same installation and configuration. The expected listing contains rewrite_module.

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

Why is my .htaccess file ignored even though mod_rewrite is loaded?

Module loading and .htaccess permission are separate. In the matching <Directory> block, AllowOverride None (or a restrictive AllowOverrideList) prevents rewrite directives. Use the narrowest policy that supports the file, normally AllowOverride FileInfo.

Should I use AllowOverride All for WordPress or another application?

Only if the application genuinely needs directives beyond the rewrite-related FileInfo class. AllowOverride All broadens what files beneath the directory can change, so prefer AllowOverride FileInfo when it is sufficient.

Should a RewriteRule pattern in .htaccess start with a slash?

No. In per-directory context Apache removes the directory prefix before matching, so use ^old-path/?$, not ^/old-path/?$.

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

Apache does not inherit rewrite configuration between virtual hosts. Check each host’s RewriteEngine On, document root, and matching <Directory> policy separately.

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.

Can I use Redirect instead of mod_rewrite?

For a simple, unconditional URL move, Redirect or RedirectMatch is simpler and more efficient. Use mod_rewrite when conditions, captures, host or query logic, or internal mapping are required.

The Bottom Line

Run sudo a2enmod rewrite, restart Apache, and verify rewrite_module. For .htaccess, separately authorize the actual document root with AllowOverride FileInfo, keep RewriteEngine On in the effective context, and remember that per-directory 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 from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

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.