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
4
votes
1 answer

.htaccess. Redirect requests to ~username folder

Situation: I'm moving a website from a production environment to a test environment. The test environment url is similar to http://192.168.1.100/~username/ There are thousands of files which use the following within the html
4
votes
2 answers

SEO URLs with ColdFusion controller?

quick ref: area = portal type page. I would like old urls http://domain.com/long/rubbish/url/blah/blah/index.cfm?id=12345 to redirect to http://domain.com/area/12345-short-title http://domain.com/area/12345-short-title should display the content. I…
Daniel Cook
  • 1,033
  • 1
  • 9
  • 19
4
votes
1 answer

Apache shows default page instead of https when using http

I'm trying to redirect all my foo.mydomain.com requests to https://foo.mydomain.com. My VirtualHost contains this: ServerName foo.mydomain.com # Indexes + root dir DirectoryIndex index.html DocumentRoot…
Jonathan Kortleven
  • 613
  • 1
  • 5
  • 16
4
votes
2 answers

Transparent redirect to port 8080

I have a few web applications available on my server on port 8080. As some of my users can't reach port 8080 due to their firewall, I'd like to set a redirection on my apache server. Il would like the following…
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
4
votes
3 answers

Apache ssl redirect using mod_rewrite

I want to do this: if they do https://example.com I want to redirect them to https://www.example.com (add the www.). I have tried oodles of things to no avail. Redirect https://example.com/ to https://www.example.com/ RewriteCond…
JJump88
4
votes
1 answer

Rewriting path for a specific file using htaccess

I would like to create a friendly url for a specific file from a directory that has a long file path as well as renaming the file. I am not even sure if this is possible. So far I have tried in htaccess: RewriteEngine On Options…
Danny Englander
  • 2,005
  • 5
  • 26
  • 41
4
votes
2 answers

Excluding images from mod_rewrite rule

Inadvertently my htaccess script is changing image URLs so that any image with "portfolio/" in its URL path is adversely affected. Is there any way to exclude images from that particular rule? redirect 301 "/sitemap.xml"…
jsuissa
  • 1,754
  • 6
  • 30
  • 64
4
votes
1 answer

mod_rewrite: QSA flag breaks configuration

I have this rewrite rule for a webservice: RewriteEngine on RewriteBase / RewriteRule ^service/(.*)$ blabla/service.php?request=$1 [L] It's working fine, but now I need to append the query string and found the QSA flag. I added it next to the L,…
bigstones
  • 15,087
  • 7
  • 65
  • 82
4
votes
1 answer

.htaccess friendly URl

Can anybody please help me with some URL rewriting? I had: (EXAMPLES) www.example.com/index.php?page=namepage www.example.com/index.php?page=gallery&topic=nametopic www.example.com/index.php?page=homepage&paging=1 I would like to…
MmM ...
  • 131
  • 1
  • 2
  • 14
4
votes
2 answers

.htaccess regex url redirection

I'm looking for a regular expression for a .htaccess file where I can redirect one URL to another. URL1: /dir/an-article-title-222 URL2: /dir/222/an-article-title I would like URL1 to 301 redirect to URL2. In highlevel language, I think I want to…
Shree
  • 77
  • 1
  • 6
4
votes
2 answers

.htaccess with -Indexes falls into infinite redirect loop when given a directory

This is my .htaccess file: # NO LISTING OF INDEXES Options -Indexes RewriteEngine On # NIX THE www BECAUSE IT IS NO LONGER 1996 AND YOU'RE COOLER THAN THAT RewriteCond %{HTTPS} !=on RewriteCond %{HTTP_HOST}…
Hoatzin
  • 1,466
  • 13
  • 14
4
votes
3 answers

Apache's mod_rewrite and %{REQUEST_URI} problem

suppose we have the following PHP page "index.php": " . $_GET['req']; ?> and the following ".htaccess" file: RewriteRule ^2.php$…
liviucmg
  • 1,310
  • 3
  • 18
  • 26
4
votes
3 answers

.htaccess files possibly conflicting?

I have one .htaccess file in the public_html folder of my server that lets me keep my primary domain in a subfolder: RewriteEngine on RewriteCond %{HTTP_HOST} ^(www.)?mrmikeanderson.com$ RewriteCond %{REQUEST_URI} !^/mrmikeanderson/ RewriteCond…
4
votes
1 answer

proxy a url using mod_rewrite

I have a domain for example http://example.com and another domain http://reallylargerdomain name.com I want if sombody access http://example.com/projects then server should proxy data from other url. For this i created following .htaccess…
kaysush
  • 4,797
  • 3
  • 27
  • 47
4
votes
1 answer

mod_rewrite best practices

So I just started messing around with Apache's rewrite module and would like to make sure I have this right because I've seen it done a few different ways. My example code is just an experiment for now but it might end up being the basic structure…
Matt Whitehead
  • 1,743
  • 3
  • 19
  • 34