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
3
votes
2 answers

Mod_Rewrite Extension Removal Changes Functioning of 404

When I try to remove my PHP extensions from all my files using my .htaccess file on my Apache server, everything works great. The extensions are removed, and everything looks much better. However, I'm having one small issue: when I would normally go…
elliottbolzan
  • 1,057
  • 1
  • 15
  • 30
3
votes
1 answer

Howto use mod_rewrite to use symfony2 with two URLs

I want to be able to access my symfony2 app with two URL's, without changing the server configuration. I tried to accomplish this by using mod_rewrite, in my case I want to be able to access my application at http://example.com/ and…
liecno
  • 924
  • 1
  • 8
  • 18
3
votes
1 answer

Apache not processing encoded URLs with %3F

The problem url links to my website are of the form /fullpage.php%3F%20cp3_Hex%3D0F0200%26cp2_Hex%3D000000%26cp1_Hex%3DFC2024 The un-encoded url is /fullpadge.php?cp3_Hex=0F0200&cp2_Hex=000000&cp1_Hex=FC2024 Apache returns a: 403: You don't…
Paul Salber
  • 700
  • 6
  • 12
3
votes
2 answers

htaccess redirect to remove index.php

I want user to be able use $_SERVER['PATH_INFO'] as a placeholder for which file to serve and get rid of index.php in the address bar For example I want to serve me.com/index.php/settings1 as me.com/settings1 and so on for any PATH_INFO that the…
qwertymk
  • 34,200
  • 28
  • 121
  • 184
3
votes
2 answers

using apache's mod_rewrite to parse SEO friendly URL's

How do I convert something like me.com/profile/24443/quincy-jones to me.com/profile.php?id=24443 or something like me.com/store/24111/robert-adams to me.com/store.php?id=24111 with mod_rewrite? Can I make the reverse conversion as well with…
daniel
  • 1,212
  • 1
  • 18
  • 33
3
votes
1 answer

mod_rewrite mysterious subreq

Seems like some apache module is interfering with my request uris as it suffixes ".html" to it. My rewrite log: 172.16.103.1 - - [08/Mar/2012:14:56:33 +0100] [www.example.org/sid#7ff723575b58][rid#7ff724b4fc58/initial] (1) pass through…
Chrisissorry
  • 1,434
  • 2
  • 22
  • 43
3
votes
1 answer

Apache RewriteRule to remove port on any domain name

I have a virtual server with one IP address, serving a few different sites. One of them has an SSL certificate on it. I needed to add an SSL cert on a second domain for private use by myself, and as I only have one IP address, I added it on port…
JoLoCo
  • 1,355
  • 2
  • 13
  • 23
3
votes
1 answer

htaccess url rewrite if file doesn't exist *OR* specific directory

I have an htaccess rewrite rule that rewrites if the file requested doesn't exist, but I would also like this rule to ignore this rule if the request falls within a specific directory. I am unsure how to do this however. My current rewrite rule is…
dqhendricks
  • 19,030
  • 11
  • 50
  • 83
3
votes
2 answers

.htaccess rewrite for language subdomains

I need to configure subdomains on my website for multilanguage support. I can set up the domains for local use pointing to a folder but my host does not allow me to point them to the main root where my app…
FFish
  • 10,964
  • 34
  • 95
  • 136
3
votes
2 answers

.htaccess rewrite folder exception

So I have the following rewrite rules: RewriteRule ([^/]*)/([^/]*)/([^/]*)$ /index.php?grandparent=$1&parent=$2&page=$3 RewriteRule ([^/]*)/([^/]*)$ /index.php?&parent=$1&page=$2 RewriteRule ([^/]*)$ /index.php?page=$1 But I need this to not…
GSto
  • 41,512
  • 37
  • 133
  • 184
3
votes
3 answers

.htaccess redirect traffic hitting .html links to TLD homepage

I'm helping a client migrate an old site which used .html extension at the end of the web address to a properly named URL structure. I want to do a redirect for all URLs that end in .html to the homepage. I tried this but it didn't…
spike5792
  • 75
  • 2
  • 2
  • 5
3
votes
1 answer

mod_rewrite: why can't environment variables be used to prevent recursion?

Why can't I use mod_rewrite rules similar to this: RewriteEngine On RewriteCond %{ENV:did_rewrite} !=true RewriteCond %{REQUEST_URI} ^(.*)/ RewriteRule (.*) %1/foo.php?original=$1 [E=did_rewrite:true] To prevent recursion? When I turn up the…
David Wolever
  • 148,955
  • 89
  • 346
  • 502
3
votes
2 answers

Problem using URL rewrite (Relative Paths not working)

I am using a .htccess file in order to rewrite my URLs. I am using the following rules in my .htaccess files Options +FollowSymlinks RewriteEngine on RewriteBase / RewriteRule ^product/([0-9]+)/([A-Za-z0-9+]+)$…
kp11
  • 2,055
  • 6
  • 22
  • 25
3
votes
1 answer

Using mod_rewrite with ErrorDocument

I am using mod_rewrite in my PHP framework to redirect user-friendly links to one main entry point of the application. The .htaccess file I am using looks like that: Options FollowSymLinks RewriteEngine On …
Pateman
  • 2,727
  • 3
  • 28
  • 43
3
votes
3 answers

mod_rewrite: Redirect request to subdir with many subfolders of different structures + redirect to index.php

I need to create a following rule to place in my .htaccess For each request i'd like to execute file/path in subfolder subdir. If the file doesn't exist there then i'd like to forward this request to index.php Let say that .htaccess iw placed at…
Sebastian Dusza
  • 2,470
  • 2
  • 30
  • 54