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

Do you have to escape a forward slash when using mod_rewrite?

With regards to the forward slash "/" when giving a regex to RewriteRule or RewriteCond, or anything else related to .htaccess in particular, is there a need to escape the forward slash? Here is an example of what I am trying to…
TrainTC
  • 253
  • 1
  • 3
  • 5
25
votes
3 answers

Any negative impacts when using Mod-Rewrite?

I know there are a lot of positive things mod-rewrite accomplishes. But are there any negative? Obviously if you have poorly written rules your going to have problems. But what if you have a high volume site and your constantly using mod-rewrite,…
gregh
  • 35,767
  • 9
  • 31
  • 27
25
votes
7 answers

htaccess force https and redirect www to non-www, but no other subdomains

I know there are many similar threads, but none of them seems to match my exact problem. Here is what I’m trying to do: (1) http://www.mydomain.com/ -> https://mydomain.com/ (2) http://mydomain.com/ -> https://mydomain.com/ (3)…
sub_lunar
  • 363
  • 1
  • 3
  • 8
24
votes
3 answers

Match Question Mark in mod_rewrite rule regex

I am looking to rewrite urls with multiple substrings. One substring is being requested as a subdirectory, while any others are requested as normal query string parameters. For example, I would like to rewrite urls…
markb
  • 3,451
  • 5
  • 24
  • 25
24
votes
2 answers

How can I use htaccess to redirect paths with a wildcard character

I have a site I recently upgraded. The old site had a calendar that created hundreds of pages, on the new site this has been replaced by an events page and those calendar URL's no longer exist. For months now I have been getting search engines…
Bryan Casler
  • 607
  • 3
  • 10
  • 20
24
votes
6 answers

mod_rewrite GUI?

Anyone have a graphical tool for developing mod_rewrite rules. Ideally it would display a pipeline of rewrites and then when given an instance of a uri would show the transforms as the get applied. It's always a pain to get them setup just right so…
Allain Lalonde
  • 91,574
  • 70
  • 187
  • 238
24
votes
3 answers

Retain original request URL on mod_proxy redirect

I am running a WebApplication on a Servlet Container (port 8080) in an environment that can be accessed from the internet (external) and from company inside (intenal),…
FrVaBe
  • 47,963
  • 16
  • 124
  • 157
24
votes
8 answers

.htaccess redirect subfolder to HTTPS

I'm trying to write a RewriteRule for my .htaccess file. Specifically, whenever a user accesses a specific subdirectory, I would like it to Rewrite to force an HTTPS connection. For example, whenever someone accesses:…
Dodinas
  • 6,705
  • 22
  • 76
  • 108
24
votes
3 answers

How to redirect to HTTPS with .htaccess on Heroku Cedar stack

I'm new to cloud hosting... I'm working on a PHP web app that's hosted on Heroku as a "Cedar" app. Heroku offers "piggy back" SSL to all their subdomains, so I can load https://myapp.herokuapp.com just fine. But I can also load…
emersonthis
  • 32,822
  • 59
  • 210
  • 375
24
votes
3 answers

.htaccess redirect domain to subdirectory without changing URL

I have the following setup: -> domain1.com is my main domain and also document root -> domain2.com is another domain which is registered as an alias domain (means it does exactly the same as domain1.com), I have multiple of those domains. when I now…
Shark5060
  • 275
  • 1
  • 2
  • 6
24
votes
2 answers

Why is Apache Permanent Redirect removing the slash between the domain and the path?

I'm using Apache 2.4, and I set up two virtual directories. One requires SSL, and the other one redirects to it. If a user attempts to visit https://www.derp.com/derp without /derp existing, they correctly get a 404. But when a user visits…
Nathan Lutterman
  • 1,855
  • 4
  • 23
  • 38
23
votes
4 answers

Custom Error 403 Page PHP

I created a .htaccess inside a directory in which I don't want the files to be directly accessed. It works and fires the default 403 page (Access forbidden!) of the Apache server. How can I create a custom 403 page? Thanks!
fart-y-goer
  • 747
  • 3
  • 8
  • 27
23
votes
2 answers

Why is it a security risk to allow encoded slashes in a URI?

I have a situation where I want encoded slashes in a URI (%2F), but my .htaccess rules are ignored when I make the request, sending me instead to a 404 page. I quickly found the Apache directive AllowEncodedSlashes, which I plan to turn on, but I…
JKS
  • 3,710
  • 2
  • 29
  • 40
23
votes
3 answers

mod_rewrite not working

I'm new to server administration but I was able to get a LAMP setup running on my new VPS. I uploaded a few web files that work on my other server, but they seem to give me the error: "File does not exist" in my /var/log/apache2/error.log file. …
RhapX
  • 1,663
  • 2
  • 10
  • 17
23
votes
3 answers

Redirect all urls exactly, just change domain name

I have a website with roughly 1K URLs. The website is moving to a different domain name. The URLs will be the exact same though, otherwise. I'd like to incorporate an htaccess or some kind of rule that does a 301 redirect for all URLs in one fell…
hdwebpros
  • 528
  • 3
  • 7
  • 20