DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober 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 Now×
Skip to content
World desk7 min

How to Enable mod_rewrite for Apache on CentOS (Step-by-Step)

A practical CentOS guide to enabling Apache mod_rewrite, choosing between virtual-host and .htaccess rules, validating changes, and troubleshooting ignored or mis-matched rewrites.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To enable mod_rewrite on CentOS, first verify that Apache has loaded the module, then turn on the rewrite engine in the configuration context where your rules will run. In a server or virtual-host block, add RewriteEngine On and your rules directly. For .htaccess, the enclosing <Directory> configuration must also permit the required overrides and options.

What enabling mod_rewrite actually involves

Apache rewriting has two separate prerequisites. The mod_rewrite module must be loaded, and the runtime rewrite engine must be enabled with RewriteEngine On. Loading the module alone does not activate rules, and adding RewriteEngine On cannot work if Apache has not loaded the module.

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.

The Apache HTTP Server documentation allows RewriteEngine in server configuration, virtual-host configuration, directory configuration and .htaccess. The engine is off by default. Rewrite settings are also not inherited by virtual hosts, so every virtual host that needs rewriting must enable it in its own configuration.

Choose the right configuration context

Use the most centralized context you control. Server or virtual-host configuration is generally easier to validate and keeps rules in Apache’s main configuration. .htaccess is appropriate when a site or directory must manage its own rules without changing the main configuration, but it depends on directory override permissions and filesystem-related options.

Configuration location Administrative control Scope Inheritance Override requirements Validation and maintenance
Server configuration Requires administrator access Can apply broadly to the server Rules are not automatically inherited by virtual hosts Not dependent on .htaccess overrides Centralized testing and reload
<VirtualHost> Requires administrator access One named virtual host Enable the engine separately in each virtual host that needs it Not dependent on .htaccess overrides Centralized per-site validation
Directory configuration Requires administrator access A directory tree Depends on Apache’s directory-processing rules Set the applicable options in the directory configuration Centralized, with directory-specific behavior
.htaccess Useful for delegated per-directory administration The directory containing the file and its descendants Controlled by the enclosing <Directory> block AllowOverride must permit the needed directives; options may also be required Less centralized and sensitive to filesystem context

Check the active CentOS Apache installation first

1. Find the configuration layout

Inspect the active httpd installation and its include structure before editing anything. Common CentOS installations use /etc/httpd/, with module declarations and additional configuration files beneath that tree, but the exact paths can differ by CentOS release, package source and local administration. Edit the file that the running service actually includes rather than creating a parallel file that Apache never reads.

2. Confirm the module is loaded

Look for the existing module declaration in the active module configuration. A typical declaration is:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
LoadModule rewrite_module modules/mod_rewrite.so

Retain the distribution’s include structure and do not add a duplicate declaration if the module is already present. On systems that provide the standard module-list command, an administrator can also inspect the loaded modules with a command such as httpd -M and look for the rewrite module. Command output and configuration paths should be verified on the specific CentOS release.

Enable rewriting in server or virtual-host configuration

This is the preferred route when you administer Apache directly. Put the engine and rules in the relevant server configuration or inside the specific <VirtualHost> block that serves the site.

1. Add the engine directive and rules

A minimal virtual-host example looks like this:

<VirtualHost *:80>
    ServerName example.test
    DocumentRoot /var/www/html

    RewriteEngine On
    RewriteRule ^old-page$ /new-page [R=301,L]
</VirtualHost>

The example redirects a request matching old-page to /new-page. Replace the host name, paths and rule with the behavior your application requires. The important activation line is RewriteEngine On; a rule without it is not processed.

2. Enable it in every virtual host that needs rules

Apache does not inherit rewrite configurations by default between virtual hosts. If one site has rewrite rules and another does not, place RewriteEngine On and the appropriate rules in each relevant virtual-host definition. A rule working in one site therefore does not prove that the module or engine is enabled for another site.

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. Test the configuration before reloading

Run the configuration-test command supplied by the installed httpd package before applying changes. On many CentOS installations this is httpd -t; use the equivalent command documented by your release if it differs. Fix every syntax or include error reported by the test.

4. Reload Apache through the service manager

After a successful configuration test, reload httpd with the service manager used by the system. A systemd-based installation commonly uses:

systemctl reload httpd

If the service manager or packaging does not support a reload operation, use the platform’s documented restart action instead. A restart interrupts active workers more than a reload, so do not substitute it until the configuration test has passed.

5. Test a URL and inspect the response

Request a URL that should match the rule. For a redirect, inspect the HTTP status and the Location response header. A command such as curl -I http://example.test/old-page can display response headers when the host is reachable. Do not treat a configuration reload as proof that the rule behaved correctly; check the actual response and consult the Apache error log if it does not match your expectation.

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

Enable mod_rewrite in .htaccess

Use this method only when per-directory delegation is required or you cannot edit the main Apache configuration. Apache ignores .htaccess completely when the enclosing <Directory> configuration contains AllowOverride None.

1. Permit the required override group

The FileInfo override group includes RewriteEngine, RewriteOptions, RewriteBase, RewriteCond and RewriteRule. The directory that contains the file must allow the group needed by your rules. An administrator-controlled directory block might therefore contain:

<Directory /var/www/html>
    AllowOverride FileInfo
</Directory>

If your rules also set an Options directive, the AllowOverride setting must permit that options group as well. A hosting provider may deliberately restrict these permissions; in that case, the provider or administrator must change the directory policy, or the rules must move into the virtual-host configuration.

2. Satisfy the per-directory symlink requirement

Per-directory and .htaccess rewriting requires an appropriate FollowSymLinks or SymLinksIfOwnerMatch option. The setting can be made in the directory configuration. If you attempt to set it in .htaccess, the applicable override permissions must allow that option.

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

Do not add this block blindly: match the path to the directory actually serving the site, preserve any existing security policy, and verify which override groups your CentOS configuration permits.

3. Create the .htaccess rules

Place the file in the directory whose requests it should govern:

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

Keep RewriteEngine On in the file even if another directory or virtual host appears to have enabled rewriting. The engine must be enabled in the context where these per-directory rules are evaluated.

4. Validate and reload

Run the same Apache configuration test used for server configuration, then reload or restart httpd through the system’s service manager. Even though the rule is in .htaccess, errors in the enclosing directory policy or included configuration can prevent Apache from starting or cause the file to be ignored.

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

Understand rule matching in each context

The text matched by a RewriteRule is not identical in every context. In directory and .htaccess context, Apache removes the directory prefix before matching the pattern. A rule in the document-root .htaccess therefore commonly matches old-page, not /old-page. A rule moved into a virtual-host or server context may need a different pattern because the request path is represented differently there.

This context difference is a frequent cause of rules that appear correct but never match. When moving a rule, re-check the path presented to the pattern, the directory in which the rule runs and whether a leading slash is present. Keep conditions and substitutions together with the rule set that depends on that context.

Troubleshoot common failures

RewriteEngine is an unknown directive

Apache has not recognized the directive because the rewrite module is not loaded in the active configuration, or the edited file is not included. Confirm the module declaration, check for duplicate or misspelled lines, verify the include path and run the configuration test again. Loading the module and enabling the engine are separate steps.

.htaccess rules are ignored

Inspect the enclosing <Directory> block first. If it uses AllowOverride None, Apache completely ignores .htaccess files in that directory. Change the policy to permit the required FileInfo directives, or move the rules into the server or virtual-host configuration. Also verify that the file is in the directory tree serving the requested URL and that its name is exactly .htaccess.

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

An options error appears for a per-directory rule

Check whether the applicable directory configuration provides FollowSymLinks or SymLinksIfOwnerMatch. If the option is declared in .htaccess, confirm that the relevant AllowOverride setting permits options. A restrictive host policy may require an administrator to make the change outside the file.

The rule works in one virtual host but not another

Enable the rewrite engine independently in the second virtual host and verify that its request is reaching the expected configuration. Rewrite settings are not inherited between virtual hosts by default, so copying a rule into one site does not activate it everywhere.

A pattern matches unexpectedly

Re-check the execution context and the path Apache supplies to the pattern. Directory and .htaccess processing removes the directory prefix before matching. A leading slash, an assumed document-root prefix or a rule copied from a virtual-host block can change the result. Test one representative URL at a time and inspect the error log when the match or substitution is unclear.

Apache will not reload after an edit

Run the configuration-test command and read the first reported error, including the file and line number. Correct syntax, missing includes, invalid directive placement or an unsupported override before attempting another reload. Do not repeatedly restart a service while the configuration test is failing.

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

Operational checklist

  • Confirm which CentOS httpd installation and include files are active.
  • Verify one, and only one, active LoadModule rewrite_module modules/mod_rewrite.so declaration.
  • Choose server, virtual-host, directory or .htaccess context deliberately.
  • Add RewriteEngine On in every context that actually runs rules.
  • For .htaccess, replace AllowOverride None only when the required delegation is acceptable.
  • Provide FollowSymLinks or SymLinksIfOwnerMatch for per-directory rewriting, with permission to set the option.
  • Account for the different pattern path used in directory and .htaccess context.
  • Run the platform’s Apache configuration test before every reload or restart.
  • Verify the expected HTTP status and, for redirects, the Location header.
  • Use the Apache error log to diagnose rules that are ignored, rejected or matching a different path.

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
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

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.