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
49
votes
8 answers

How to hide the .html extension with Apache mod_rewrite

I have a small number of static sites where I simply want to hide the .html extension: the URL /foo fetches the static file /foo.html the browser still displays the URL /foo The client can then send out bookmarks in the style example.com/foo…
Dave Everitt
  • 17,193
  • 6
  • 67
  • 97
48
votes
2 answers

htaccess 301 redirect - Remove query string (QSA)

I've been struggling with some htaccess redirects. I just spent some time reading and searching on stack and couldn't get an anwser that works with my scenario. I'm in the process of making the 301 redirect for an old client website to a new one.…
newpoison
  • 914
  • 2
  • 8
  • 20
46
votes
2 answers

Redirect all traffic to root of another domain

I have a domain that's not to be used anymore. I want to redirect all from http://www.old.com/ to http://www.new.com/, no matter what page the user's attempted to access on www.old.com. Doing this: RewriteEngine on Redirect 301 /…
user527892
46
votes
6 answers

apache mod_rewrite is not working or not enabled

I have installed rewrite_module and modified php.ini on Apache. I create rewrite.php and .htaccess files, but it's not working. My filesystem folders…
rc1021
  • 703
  • 1
  • 7
  • 16
46
votes
5 answers

Single ErrorDocument directive to catch all errors (.htaccess)

Is there something like a wildcard directive to catch all possible errors and deal with them in a single custom error page? ErrorDocument 404 /error.php?code=404 ErrorDocument 403 /error.php?code=403 ... ErrorDocument NNN /error.php?code=NNN…
mkvlrn
  • 758
  • 1
  • 8
  • 20
46
votes
1 answer

What exactly does the Multiviews options in .htaccess?

I've been struggling a lot with an access rule that needed to rewrite one piece of URL adding a path. RewriteRule ^(configuration/.+)$ application-server/$1 [L,NC,R=301,NE] This Rule caused just a blank page on my Joomla site with no error log or…
user2824073
  • 2,407
  • 10
  • 39
  • 73
45
votes
2 answers

Proxying with SSL

I have a Linux host running Apache and a Windows host running IIS. I have a domain that points to the Linux host and need to relay (proxy) requests for it to IIS; I thus have the following virtual host definition in Apache (which works just…
ekkis
  • 9,804
  • 13
  • 55
  • 105
45
votes
13 answers

No input file specified

I'm running Anchor CMS and I just upgraded to version 0.8. When I try and run the installer I get a 'No input file specified' error. I believe it's more than likely a .htaccess problem but I'm not sure what the correct settings should be. My site…
Cole Roberts
  • 954
  • 3
  • 15
  • 25
44
votes
6 answers

$ service apache2 restart [fail]

I have installed Apache on my Ubuntu Server. For a special reason I have to enable mod_rewrite on it. So I have done this. And in every Tutorial on the internet the last command is to restart apache. But when I do this the console prints…
Miralem Cebic
  • 1,276
  • 1
  • 15
  • 28
44
votes
5 answers

symfony2 rewrite rules .htaccess app.php

I uploaded my symfony2 project to server grove. The main page loads, but all the links are broken. I tried adding app.php to the web address. It did work, but: I have routes like this one: www.something.com/app.php/something I want them to…
Francisco Ochoa
  • 1,538
  • 4
  • 19
  • 43
43
votes
1 answer

Difference between $1 vs %1 in .htaccess

What is the difference between %1 and $1 in .htaccess? For example, # to remove www RewriteCond %{HTTP_HOST} ^(\w+)\.mydomain\.com [NC] RewriteRule .* http://mydomain.com/%1 [R=301,L] # versus # RewriteRule .*…
csi
  • 9,018
  • 8
  • 61
  • 81
43
votes
2 answers

"OR" Flag in .htaccess mod_rewrite

Just found this .htaccess rewrite code RewriteEngine on RewriteCond %{HTTP_HOST} ^my.domain.com$ [NC,OR] RewriteCond %{REQUEST_URI} !public/ RewriteRule (.*) /public/$1 [L] And I was wondering what was the purpose of the "OR" flag. Already checked…
gabriel-kaam
  • 1,230
  • 1
  • 12
  • 13
40
votes
5 answers

using header() to rewrite filename in URL for dynamic pdf

I have a php script that generates a pdf report. When we go to save the pdf document, the filename that Acrobat suggests is report_pdf, since the php script is named report_pdf.php. I would like to dynamically name the pdf file, so I don't have to…
user151841
  • 17,377
  • 29
  • 109
  • 171
40
votes
1 answer

mod_rewrite rule to redirect all requests except for one specific path

I'm trying to redirect all requests to my domain to another domain using mod_rewrite in an Apache 2.2 VirtualHost declaration. There is one exception to this -- I'd like all requests to the /audio path not to be redirected. I've written a…
Olly
  • 7,732
  • 10
  • 54
  • 63
40
votes
3 answers

Apache mod_rewrite: force www only if not in localhost

I have the following in my htaccess to force the www in URLs: RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] How do I only apply this if not on localhost? Is there some sort of if-condition I can put? Right…
StackOverflowNewbie
  • 39,403
  • 111
  • 277
  • 441