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

Is it possible to limit the number of $_GET parameters to a Zend Framework application using Zend_Cache_Backend_Static to cache static pages as HTML

I've just set up static page caching using Zend_Cache_Backend_Static to serve cached html files in my application, which is working great. The only concern I have is down to the way it caches files with $_GET parameters. Because it automatically…
baseten
  • 1,362
  • 1
  • 13
  • 34
3
votes
1 answer

mod_rewrite and HTTP Basic auth

I've got WordPress installed in the root directory of my web server, and it puts the following rewrite rules into the root .htaccess: RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond…
Matt Lowe
  • 43
  • 1
  • 5
3
votes
4 answers

CSS styles disappear if I type an argument after index.php

My CSS styles stop working if I type any argument following my landing page index.php I'm adding/testing new code on my development localhost server running xampp, coding in php/javascript/html/css. EDIT: my development server is Windows Vista, and…
wantTheBest
  • 1,682
  • 4
  • 43
  • 69
3
votes
1 answer

Apache basic authentication SSL only

I've been given a setup in which Apache runs on Windows, and we have two folders that need basic authentication with .htpasswd. First, I tested that the authentication worked: AuthUserFile E:/path-to/.htpasswd AuthType Basic AuthName "Secure…
Jonathan Stegall
  • 530
  • 1
  • 6
  • 23
3
votes
5 answers

Apache Rewrite: favicon in %{HTTP_HOST} directory

My software supports multiple domains pointed at the exact same directory, I use shared hosting. I need to have each domain's favicon load from directories with their respective host names. Here is a…
John
  • 1
  • 13
  • 98
  • 177
3
votes
2 answers

Two Part SEO Friendly URL

I'm in need of an SEO friendly URL for something small. The current url looks like mysite.com/ny.php?c=[COUNTY]&t=23 With 'c' being a county name, and 't' being the id number of a town. I found this solution in a previous question:
Tim
  • 1,199
  • 1
  • 8
  • 14
3
votes
2 answers

htaccess mod_rewrite part of url to GET variable

I have some URLs like these: http://plutov.by/post/cubique_zf_jquery http://plutov.by/post/mysql_useful_queries How can I with help of Apache mod_rewrite open the next…
Alex Pliutau
  • 21,392
  • 27
  • 113
  • 143
3
votes
2 answers

How can I set up a reverse proxy with mod_proxy without redirecting?

How can I set up a reverse proxy with mod_proxy without redirecting to another server or IP? This will be a virtual host environment. The reason I want to do this is so that mod_proxy handles the communication with the client's browser thereby…
3
votes
1 answer

Apache rewriterule and rewritecond with 3 domains

I have three domains (domain1.es, domain2.com, domain3.com). I need that: domain1.es OR www.domain1.es, show the content of www.domain1.es/catalog domain2.com OR www.domain2.com, redirect to www.domain1.es and show the content of…
Nerque
  • 33
  • 1
  • 3
3
votes
1 answer

Removing index.php from website URLs

I am using the Kohana framework (but I think it's irrelevant for this question) and pages can be accessed like so http://www.example.com/articles/ http://www.example.com/index.php/articles/ Now, as a rule of thumb, I generally try and tweak my…
alex
  • 479,566
  • 201
  • 878
  • 984
3
votes
2 answers

Use long list of .htaccess RewriteRule or use a single catchall and direct in PHP?

Say im using .htaccess file for clean looking links, but that I have a long list (say 20) of RewriteRules to do things like this domain.com/user/adam --> domain.com/user.php?need=user&user=adam domain.com/test.js -->…
Adam Meyer
  • 1,505
  • 1
  • 19
  • 28
3
votes
4 answers

mod rewrite to remove file extension, add trailing slash, remove www and redirect to 404 if no file/directory is available

I would like to create rewrite rules in my .htaccess file to do the following: When accessed via domain.com/abc.php: remove the file extension, append a trailing slash and load the abc.php file. url should look like this after rewrite:…
ximi
  • 596
  • 5
  • 17
3
votes
1 answer

Redirecting Apache by Geo/Country IP

Do any of you folks do redirection by IP (within Apache) for people coming to your website from different countries? What do ye use? mod_geoip and/or a whole pile of mod_rewrite rules? Is mod_geoip reliable/well-performing/accurate?
Derek Organ
  • 8,323
  • 17
  • 56
  • 75
3
votes
2 answers

Rewrite for all URLs

I would like to rewrite something like: http//www.example.com/index.php?var1=val1&var2=val2&var3=val3 Into http://www.example.com/var1/val1/var2/val2/var3/val3/ I'm looking for a solution to work on an arbitrary number of variables. Can it be done?
Paul Grigoruta
  • 2,386
  • 1
  • 20
  • 25
3
votes
3 answers

.htaccess excluding rewrite css and images

I have some problems with rewriting URL-s my .htacces file: RewriteEngine On RewriteBase /bsc/ RewriteRule ^(.+/?)$ index.php?url=$1 [NC,QSA,L] my file and folder…
Miroslav
  • 33
  • 1
  • 1
  • 3