Questions tagged [mod-rewrite]

URL rewriting module for the Apache web server. It is commonly used for so-called `pretty URLs` , but also provides the power and flexibility to perform various request handling tasks beyond simple substitutions.

Introduction to mod_rewrite

mod_rewrite is an module which allows requests to be transformed server-side. It can be used to perform both internal and external redirects, and can provide conditional hooks into functionality provided by other modules (like mod_proxy). Comprehensive reference and supplementary documentation with several examples are available from the Apache documentation website, in addition to the many solutions to common and advanced mod_rewrite issues available here.

A comprehensive introduction into mod_rewrite

Everything You Ever Wanted to Know about Mod_Rewrite Rules but Were Afraid to Ask?

Common mod_rewrite usage scenarios and questions

How does mod_rewrite work?

mod_rewrite works its magic by applying a series of rules and conditions to the requested URL, performing substitutions when matching succeeds. The majority of this work is done by the RewriteRule and RewriteCond directives which can be defined in server (or virtual host) configurations or in .htaccess files. This allows mod_rewrite to be used in a variety of deployment scenarios, including shared hosting where it is often installed by default.

Using the directives and variables* available, it's possible to condition rewrites on a reasonably complex set of conditions - including negated regular expressions and literal matches. As a simple example to demonstrate some of these features, consider performing an internal redirect to an alternate index file if a subdomain was requested:

RewriteEngine On                           # Turn on the engine

RewriteCond %{HTTP_HOST}    =s.example.com # Test for subdomain
RewriteCond %{REQUEST_URI} !^/.+$          # Test if the request URI is empty
RewriteRule ^ /index-alt.html [QSA]        # Internal rewrite with QSA flag*

*A full list of variables is available in the RewriteCond section of the documentation. Additionally, a list of rule flags is available in the RewriteRule section.

mod_rewrite is not without its quirks though, especially when it comes to differences between defining rules in a server configuration and in a .htaccess file. When possible, use the RewriteLog directive to see what's going on under the hood, and when in doubt, ask the Stack Overflow community!

Do I need mod_rewrite?

mod_rewrite is a complex and powerful tool. As such, for some simple use cases there are simpler and occasionally faster alternatives. The Apache wiki documents some of these use cases in their When Not To Use Rewrite page.

Asking a new mod_rewrite question

There are many users here who are happy to help demystify mod_rewrite. To get the best possible answers, it's recommended that questions include all of the rules, a mention of whether they are defined in httpd.conf or .htaccess, an example of what should happen (but doesn't; e.g. *"`/home` should go to `home.php`"*), and, finally, a description of what "doesn't work" about it (for instance, *"Apache returns a 404 error message"*). Happy rewriting!

Resources and Links

33051 questions
554
votes
25 answers

Generic htaccess redirect www to non-www

I would like to redirect www.example.com to example.com. The following htaccess code makes this happen: RewriteCond %{HTTP_HOST} ^www\.example\.com [NC] RewriteRule ^(.*)$ http://example.com/$1 [L,R=301] But, is there a way to do this in a generic…
deepwell
  • 20,195
  • 10
  • 33
  • 39
545
votes
15 answers

How to enable mod_rewrite for Apache 2.2

I've got fresh install of Apache 2.2 on my Vista machine, everything works fine, except mod rewrite. I've uncommented LoadModule rewrite_module modules/mod_rewrite.s but none of my rewrite rules works, even simple ones like RewriteRule not_found…
Jakub Arnold
  • 85,596
  • 89
  • 230
  • 327
332
votes
14 answers

htaccess redirect to https://www

I have the following htaccess code: RewriteEngine On RewriteCond !{HTTPS} off RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$…
bigben
  • 3,381
  • 4
  • 15
  • 8
308
votes
19 answers

Tips for debugging .htaccess rewrite rules

Many posters have problems debugging their RewriteRule and RewriteCond statements within their .htaccess files. Most of these are using a shared hosting service and therefore don't have access to the root server configuration. They cannot avoid…
TerryE
  • 10,724
  • 5
  • 26
  • 48
278
votes
19 answers

How can I use .htaccess rewrite to redirect root URL to subdirectory?

Trying to get www.example.com to go directly to www.example.com/store I have tried multiple bits of code and none work. What I've tried: Options +FollowSymlinks RewriteEngine on RewriteCond %{HTTP_HOST} ^example.com$ RewriteRule (.*)…
AlphaSmith
277
votes
20 answers

.htaccess redirect all pages to the homepage on a new domain

Which redirect rule would I use to redirect all pages under olddomain.example to be redirected to newdomain.example? The site has a totally different structure, so I want every page under the old domain to be redirected to the new domain index…
Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
254
votes
8 answers

Force SSL/https using .htaccess and mod_rewrite

How can I force to SSL/https using .htaccess and mod_rewrite page specific in PHP.
Sanjay Shah
  • 2,809
  • 2
  • 19
  • 20
248
votes
9 answers

How does RewriteBase work in .htaccess

I have seen this in a few .htaccess examples RewriteBase / It appears to be somewhat similar in functionality to the of HTML. I believe it may automatically prepend its value to the beginning of RewriteRule statements (possibly ones…
alex
  • 479,566
  • 201
  • 878
  • 984
234
votes
26 answers

apache redirect from non www to www

I have a website that doesn't seem to redirect from non-www to www. My Apache configuration is as follows: RewriteEngine On ### re-direct to www RewriteCond %{http_host} !^www.example.com [nc] RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]…
user121196
  • 30,032
  • 57
  • 148
  • 198
207
votes
5 answers

How to debug Apache mod_rewrite

I have two main problems with mod_rewrite: There is no meaningful error reported when I have an invalid rule To reliably test each modification, I have to erase Google Chrome's cache. This isn't rocket science, but I have to hit Ctrl + Shift +…
puk
  • 16,318
  • 29
  • 119
  • 199
177
votes
18 answers

How to remove .html from URL?

How to remove .html from the URL of a static page? Also, I need to redirect any url with .html to the one without it. (i.e. www.example.com/page.html to www.example.com/page ).
Dave
  • 3,328
  • 7
  • 26
  • 30
175
votes
8 answers

Redirect all to index.php using htaccess

I am writing a simple PHP-based MVC-ish framework. I want this framework to be able to be installed in any directory. My PHP script grabs the request uri and breaks it off into segments. It makes segment 1 the controller and segment 2 the action.…
David Ericsson
  • 2,570
  • 2
  • 19
  • 33
161
votes
5 answers

Reference: mod_rewrite, URL rewriting and "pretty links" explained

"Pretty links" is an often requested topic, but it is rarely fully explained. mod_rewrite is one way to make "pretty links", but it's complex and its syntax is very terse, hard to grok, and the documentation assumes a certain level of proficiency in…
deceze
  • 510,633
  • 85
  • 743
  • 889
160
votes
6 answers

.htaccess mod_rewrite - how to exclude directory from rewrite rule

I have 8 lines of rewrite rules in my .htaccess file. I need to exclude two physical directories on my server from these rules, so they can become accessible. For now all requests are sent to index.php file. Directories to exclude: "admin" and…
Kelvin
  • 8,813
  • 11
  • 38
  • 36
155
votes
5 answers

URL rewriting with PHP

I have a URL that looks like: url.com/picture.php?id=51 How would I go about converting that URL to: picture.php/Some-text-goes-here/51 I think WordPress does the same. How do I go about making friendly URLs in PHP?
Jazerix
  • 4,729
  • 10
  • 39
  • 71
1
2 3
99 100