Enable It on Ubuntu and Debian
sudo a2enmod rewrite
sudo apachectl configtest
sudo systemctl restart apache2a2enmod links the module into mods-enabled and prints a reminder to run systemctl restart apache2; follow it for new modules. Apache's documentation says a graceful restart also re-reads the whole configuration, but the restart is the packaging's own advice. For later changes to the rules themselves, a reload is enough: it applies the new configuration while running requests finish.
Check That It Is Loaded
sudo apache2ctl -M | grep rewrite # Debian and Ubuntu
sudo httpd -M | grep rewrite # AlmaLinux and Rocky LinuxA line reading rewrite_ means it is active. On AlmaLinux and Rocky Linux, apachectl does not accept -M, so call httpd directly. There the module is loaded by a LoadModule line in /etc/httpd/conf.modules.d/00-base.conf; if it is missing from the output, check that this line has not been commented out, then run sudo httpd -t and restart httpd.
Where the Rules Go
Rewrite rules work in the virtual host and in .htaccess files. For .htaccess, the virtual host must allow them with AllowOverride FileInfo or All, and the folder needs Options FollowSymLinks or SymLinksIfOwnerMatch; without one of them, Apache answers 403 Forbidden and logs that the RewriteRule directive is forbidden. See what is .htaccess.
One detail trips people up: in the virtual host, the path a pattern sees starts with a slash. In .htaccess and inside a Directory block, the folder's prefix is removed and there is no leading slash.
RewriteRule Basics
RewriteEngine On
RewriteCond test-string condition
RewriteRule pattern substitution [flags]RewriteEngine On is needed once in each context. A RewriteCond applies to the RewriteRule right after it. The pattern is a regular expression matched against the path.
| Flag | Meaning |
|---|---|
L | Stop after this rule |
R= | Permanent redirect; R= is temporary |
NC | Ignore case |
QSA | Keep the query string and add to it |
F | Answer 403 Forbidden |
Examples
Redirect www to the Bare Domain
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,R=301]This works in both places, because it uses REQUEST_ instead of the matched path.
Move a Section
# In .htaccess in the document root
RewriteRule ^blog/(.*)$ https://example.com/news/$1 [L,R=301]In the virtual host, the pattern becomes ^/blog/(.*)$.
Send Everything Else to index.php
# In .htaccess or a Directory block
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]Existing files and folders are served as they are; every other address goes to the application's front controller.
Test with curl
curl -I http://example.com/blog/first-postA redirect shows a 301 or 302 status and a Location header with the new address. Use R=302 while you test: browsers remember a 301 and keep following it after you change the rule.
Log What mod_rewrite Does
Apache 2.4 has no RewriteLog directive; rewrite tracing goes to the error log through LogLevel. Add this line to the virtual host, test, and reload:
LogLevel warn rewrite:trace3Each request then writes lines marked rewrite:trace3 that show the input, each pattern tried, and the result:
sudo tail -f /var/log/apache2/error.log | grep rewrite # Debian and Ubuntu
sudo tail -f /var/log/httpd/error_log | grep rewrite # AlmaLinux and Rocky LinuxThe levels run from trace1 to trace8; Apache's documentation warns that levels above trace2 slow the server down a lot. Remove the line and reload once the rule works. Log paths for both families are in how to read Apache access and error logs.
Common Problems
- Rules are ignored: the .htaccess file is not read because of
AllowOverride None, orRewriteEngine Onis missing. - 500 error: the module is not loaded, or a rule has a syntax error; the error log says which.
- Redirect loop: a rule matches its own target. Add a RewriteCond that excludes it; see ERR_
TOO_ .MANY_ REDIRECTS
Updated:
Linux VPS with Full Root Access
MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.
More Guides
No Limits on Service or Support
You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

- 100% customer satisfaction goal
- Our own hardware
- 24/7 ticket and email support
- 4 countries with locations