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
29
votes
18 answers

.htaccess & WordPress: Exclude folder from RewriteRule

I have this .htaccess file in WordPress. It's located at /public_html/ (web root). I need to exclude a folder (csNewsAd) from the rewrite engine. I've tried this, based from another question similar here at SO, but didn't work at all. AddHandler…
29
votes
1 answer

mod_rewrite: what does this RewriteRule do?

Given these conditions (I know what they mean/do): RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d What does the first rule do? What is that lonely dash for? RewriteRule ^.*$ -…
Decent Dabbler
  • 22,532
  • 8
  • 74
  • 106
28
votes
3 answers

ErrorDocument 404 /404.php is not working in .htaccess file in PHP

I have a .htaccess file in the root directory and also 404.php file there. Content of my .htaccess file is: ErrorDocument 404 /404.php But when I am mis-spelling my url, 404.php is not opening. Instead I am getting following message: Not…
user1556433
27
votes
2 answers

Different folder as website subfolder

I need some help with URL rewriting. The case is similar with this one. I have a working Zend Framework site. Now I must add a blog in Wordpress (also working). I've chosen not to indulge in ZF controller-/action-/route-making; I've seen a couple of…
nevvermind
  • 3,302
  • 1
  • 36
  • 45
27
votes
6 answers

.htaccess redirect from site root to public folder, hiding "public" in URL?

Bellow is my site directory structure: htdocs/ My-Project/ public/ index.php css/ img/ js/ src/ config.php templates/ library/ I do not want…
tdc
  • 5,174
  • 12
  • 53
  • 102
26
votes
2 answers

mod_rewrite or mod_alias?

I have a server, its httpd.conf already has some "RedirectMatch permanent" directives in it. I'm not that familiar with mod_alias, I've only ever used mod_rewrite. What's the basic difference? I don't see a "L" flag in mod_alias to stop processing…
jeph perro
  • 6,242
  • 26
  • 90
  • 124
26
votes
3 answers

Block by useragent or empty referer

A stranger bot (GbPlugin) is codifying the urls of the images and causing error 404. I tried to block the bot without success with this in the bottom of my .htaccess, but it didn't work. Options +FollowSymlinks RewriteEngine On RewriteBase / …
Vera
  • 391
  • 1
  • 4
  • 9
26
votes
5 answers

mod_rewrite: remove trailing slash (only one!)

I use mod_rewrite/.htaccess for pretty URLs. I'm using this condition/rule to eliminate trailing slashes (or rather: rewrite to the non-trailing-slash-URL, by a 301 redirect; I'm doing this to avoid duplicate content and because I like URLs with no…
user367217
  • 499
  • 2
  • 6
  • 20
26
votes
4 answers

Set RewriteBase to the current folder path dynamically

Is there any way to set RewriteBase to the path current folder (the folder which the .htaccess file is in) relative to the host root? I have a CMS and if I move it to the directory in my host it does not work unless I set the RewriteBase to the path…
Eeliya
  • 1,534
  • 5
  • 23
  • 37
26
votes
9 answers

.htaccess not working on localhost with XAMPP

i m using XAMPP but i m not able to use .htaccess file at local host. i m trying so many times.. Online working good. but local host showing [The requested URL was not found on this server] My root folder is…
acre
  • 283
  • 2
  • 4
  • 6
26
votes
5 answers

rewrite rules for apache 2 to use with angular js

Obviously, there are a lot of mod rewrite discussions and answers all across the web. However, I am having a hard time grasping them. So I thought I would ask here. I'm asking for rewrite rules to do what Andy Joslin explained in the comments here:…
Mike Haas
  • 2,273
  • 3
  • 25
  • 30
26
votes
2 answers

What does the $1 argument in this RewriteCond do?

I'm setting up Apache rewrite rules to tidy up my CodeIgniter URLs. This question (and lots of forum posts etc that I've found around the place) document the use of the following rule (or something very similar): RewriteEngine on RewriteCond $1…
Highly Irregular
  • 38,000
  • 12
  • 52
  • 70
26
votes
2 answers

How to make .htaccess RewriteCond check domain name?

I have this rule: RewriteRule ^(about|installation|mypages|privacy|terms)(/)*$ /index.php?kind=portal&id=1&page=$1&%{QUERY_STRING} [L] How can I change it so that it would work only for a specific domain, www.domain.com for example?
D_R
  • 4,894
  • 4
  • 45
  • 62
25
votes
3 answers

How to debug htaccess rewrite script

I was wondering how to create and debug this kind of script that can become a bit of headache if you are not used to write them (like me). Do you use tool to create them? Any tips to debug what's going on instead of just create a local structure and…
Soundstep
  • 618
  • 2
  • 6
  • 11
25
votes
10 answers

mod_rewrite equivalent for IIS 7.0

Is there a mod_rewrite equivalent for IIS 7.0 that's a) more or less complete b) suitable for a production environment, i.e. battle-tested/dependable/secure Do you have an experience-based recommendation?
John Mac
  • 2,530
  • 4
  • 24
  • 30