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

Apache 2.2 Change HTTP Request Method From POST to GET

Is there a way I can change the HTTP Request Method from POST to GET using apache modules such as mod_rewrite or 3rd party module ? The reason I'm doing this is to minimize server side changes as I'm doing this for a demo.
Lydon Ch
  • 8,637
  • 20
  • 79
  • 132
3
votes
1 answer

htaccess rewriting twice in CHROME

I have the following in my htaccess so i can access the page as via domain.com/whatever instead of domain.com/index.php/view/whatever. So this removes index.php/view from the URL. This is working fine in all browser except chrome. Chrome is…
Pinkie
  • 10,126
  • 22
  • 78
  • 124
3
votes
3 answers

.htaccess 404 error on real path

I want the following: www.bla-bla.com/ --> www.bla-bla.com/php/index.php www.bla-bla.com/php/index.php --> www.bla-bla.com/php/error.php I have tried the following but doesn't work RewriteEngine on RewriteRule ^/?$ /php/index.php…
Chris P
  • 2,059
  • 4
  • 34
  • 68
3
votes
1 answer

mod_rewrite to shorten url files path

I am having a bit of difficulty getting mod_rewrite to do what I need it to do. We have a group of virtual subdomains in a Drupal install. So, academics.univ.edu, about.univ.edu, etc are all part of the same core Drupal install. File access…
3
votes
1 answer

Rewrite to relative path in Apache2 on Mac OS X Lion (10.7)

I've searched the site thoroughly and Googled for this as well, but to no avail. I use Apache2 + PHP on my Mac OS X. I haven't changed much of the configuration on any of them, just enough to get everything working correctly. Here's my .htaccess…
Milad Naseri
  • 4,053
  • 1
  • 27
  • 39
3
votes
1 answer

How does Zend parse URL in a front controller using mod_rewrite?

I can't figure out how the Zend Framework controls routes. I am actually trying to create my own controller/route class, wrote the same code as Zend reccomends for .htaccess to rewrite to an index.php located in the root of the…
binar
  • 1,197
  • 1
  • 11
  • 24
3
votes
2 answers

mod_rewrite help to pass full path with query string

I'm doing some caching based on the whole url, including query string, and need Apache to recognize the file and use it. It seems to find a match but then strips the query string off before rendering the file. url:…
Jason Harrison
3
votes
0 answers

RewriteRule for multiple robots.txt

I have a site with multiple domains and i need a robots.txt for each domain. For example: example.com will have robots-com.txt example.se will have robots-org.txt example.co.uk will have robots-co.uk.txt etc... And the URL would be…
Patrik
  • 2,207
  • 3
  • 30
  • 48
3
votes
5 answers

Point an tag to a image dynamically generated by PHP?

Is it possible to redirect an image to a dynamically generated image(using PHP)? I have a dynamically created image and it has an extension ".PHP" (obviously) and that server is not under my control. So I want to redirect "somename.jpg" (on my…
apnerve
  • 4,740
  • 5
  • 29
  • 45
3
votes
2 answers

.htacces css/js subdirectory issue

I am trying to make some changes to .htaccess file of a site. I have searched all over the web, but I cant find a clear solution for that, or there is possibility that I have got it wrong. So here we are are: RewriteEngine on RewriteCond…
kokazani
  • 213
  • 2
  • 13
3
votes
1 answer

htaccess mod_rewrite, difference between [L] and [R=301,L]

So what is the difference? Will the browser cache the 301 and remember it?
Tyilo
  • 28,998
  • 40
  • 113
  • 198
3
votes
2 answers

Cannot get started with mod_rewrite

I have in the past successfully managed to copy/edit/paste rewrite rules on an apache server over which I had limited control. Now however I am experimenting with an Apache server which I can restart and configure to my heart's content but I cannot…
asparagus
  • 55
  • 2
3
votes
3 answers

How do I redirect one subdomain to another, when they both point to the exact same files?

I have a site that has two domains pointing to it, let's call them: work.mysite.com play.mysite.com This is bad practice, so I want to choose work.mysite.com and make it the canonical URL, permanently redirecting play.mysite.com to it. I'm in the…
rhodesjason
  • 4,904
  • 9
  • 43
  • 59
3
votes
1 answer

rewriting URLs from .json to .php

I want http://server/path/app.json?a=foo&b=bar to map to http://server/path/foo.php?a=foo&b=bar using mod_rewrite. I have the following incantation in my .htaccess which doesn't give any joy RewriteEngine On RewriteRule ^([^.?]+).json(.*)$ $1.php$2…
punkish
  • 13,598
  • 26
  • 66
  • 101
3
votes
1 answer

.htaccess rewrite rule won't unicode characters

I am using the following ModRewrite to make my urls look cleaner: RewriteEngine On RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?key=$1 It allows use of letters and numbers just fine, but it produces a 400 error when i try to use %, which I require to…
Joshua Davis
  • 325
  • 7
  • 15