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
152
votes
15 answers

How to check if mod_rewrite is enabled in php?

I was wondering if it is possible to check if mod_rewrite is enabled on Apache AND IIS in PHP. ModRewrite for IIS exists. Check it here. So, I'm looking for a PHP script that checks for mod_rewrite on Apache and IIS. Does anyone know such script or…
Ties
  • 1,776
  • 2
  • 12
  • 17
143
votes
8 answers

deny direct access to a folder and file by htaccess

Here is the scenario: There is a index.php file in root folder some files are included in index.php which are in the includes folder. 1 other file (submit.php) is in the root folder for form submit action. I want to restrict direct user access to…
Imrul.H
  • 5,760
  • 14
  • 55
  • 88
134
votes
19 answers

How to check whether mod_rewrite is enable on server?

Currently I am using the hosting with lightspeed server. Hosting says mod_rewrite is enabled but I can't get my script working there. Whenever I try to access the URL, it returns 404 - not found page. I put the same codes at another server which is…
knightrider
  • 2,533
  • 7
  • 30
  • 42
133
votes
2 answers

how to use "AND", "OR" for RewriteCond on Apache?

Is this how to use AND, OR for RewriteCond on Apache? rewritecond A [or] rewritecond B rewritecond C [or] rewritecond D RewriteRule ... something becomes if ( (A or B) and (C or D) ) rewrite_it. So it seems like "OR" is higher precedence than…
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
133
votes
18 answers

Remove .php extension with .htaccess

Yes, I've read the Apache manual and searched here. For some reason I simply cannot get this to work. The closest I've come is having it remove the extension, but it points back to the root directory. I want this to just work in the directory that…
Dirty Bird Design
  • 5,333
  • 13
  • 64
  • 121
127
votes
34 answers

Getting a 500 Internal Server Error (require() failed opening required path) on Laravel 5+ Ubuntu 14.04

I have installed Laravel many times on Windows OS but never had this problem. However, on Ubuntu 14.04 I am getting a 500 Internal Server Error, and messages like this in my logs: [Wed Jul 22 10:20:19.569063 2015] [:error] [pid 1376] [client…
DpEN
  • 4,216
  • 3
  • 17
  • 26
126
votes
9 answers

How to debug .htaccess RewriteRule not working

I have a RewriteRule in a .htaccess file that isn't doing anything. How do I troubleshoot this? How can I verify if the .htaccess file is even being read and obeyed by Apache? Can I write an echo "it is working" message, if I do write it, where…
macha
  • 7,337
  • 19
  • 62
  • 84
118
votes
8 answers

Hidden features of mod_rewrite

There seem to be a decent number of mod_rewrite threads floating around lately with a bit of confusion over how certain aspects of it work. As a result I've compiled a few notes on common functionality, and perhaps a few annoying nuances. What other…
Owen
  • 82,995
  • 21
  • 120
  • 115
116
votes
27 answers

How to remove "index.php" in codeigniter's path

How do I remove the "index.php" sticking out in every path in codeigniter somewhere in the center? I want clean non index.php-fied URLs?
OrangeRind
  • 4,798
  • 13
  • 45
  • 57
112
votes
8 answers

.htaccess - how to force "www." in a generic way?

This will change domain.example to www.domain.example: # Force the "www." RewriteCond %{HTTP_HOST} !^www\.domain\.example$ [NC] RewriteRule ^(.*)$ `http://www.domain.example/$1` [R=301,L] How do I replace the "domain" part so that this works on any…
StackOverflowNewbie
  • 39,403
  • 111
  • 277
  • 441
103
votes
7 answers

Do you have to restart apache to make re-write rules in the .htaccess take effect?

I have pushed my .htaccess files to the production severs, but they don't work. Would a restart be the next step, or should I check something else.
Jesse Hattabaugh
  • 7,876
  • 8
  • 34
  • 38
97
votes
4 answers

Htaccess: add/remove trailing slash from URL

My website runs a script called -> WSS wallpaper script My Problem -> I have been trying to force remove or add trailing slash to the end of my URL to prevent duplicated content and also to clean up my URLs. I have tried all sorts and tried…
Luke
  • 973
  • 1
  • 7
  • 5
93
votes
11 answers

Header set Access-Control-Allow-Origin in .htaccess doesn't work

I can't figure out why my .htaccess header settings doesn't work. My .htaccess file content: Header set Access-Control-Allow-Origin * Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT" Header always set…
user1401592
  • 939
  • 1
  • 7
  • 4
90
votes
23 answers

How to force https on elastic beanstalk?

I can't seem to force https on the free usage tier of elastic beanstalk. I have tried the following suggestion at How to force https on amazon elastic beanstalk without failing the health check Using this Apache rewrite rule RewriteEngine…
82
votes
3 answers

When setting environment variables in Apache RewriteRule directives, what causes the variable name to be prefixed with "REDIRECT_"?

I am trying to set Apache environment variables (for use in PHP) with the [E=VAR:VAL] flag on RewriteRule rules in an .htaccess file. I have already discovered the variables are accessed in PHP as server variables $_SERVER rather than $_ENV (which…
trowel
  • 961
  • 1
  • 8
  • 8