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
75
votes
11 answers

How do I ignore a directory in mod_rewrite?

I'm trying to have the modrewrite rules skip the directory vip. I've tried a number of things as you can see below, but to no avail. # BEGIN WordPress RewriteEngine On RewriteBase / #RewriteRule ^vip$ - [PT] RewriteRule…
user24557
  • 871
  • 1
  • 7
  • 6
70
votes
7 answers

How to prevent a file from direct URL Access?

I'm using Apache and I have a sample web folder on my Local Host, like: http://localhost/test/ Files in the test folder: index.html sample.jpg .htaccess Sample source of index.html:
夏期劇場
  • 17,821
  • 44
  • 135
  • 217
69
votes
5 answers

.htaccess Remove WWW from URL + Directories

This seems to be a non-issue for many people (read: I can't find an answer), but I would like to update the following htaccess code to not only remove the 'www' from the URL, but also any sub-directories that are accessed. RewriteEngine…
Cycododge
  • 943
  • 1
  • 8
  • 10
68
votes
6 answers

Forbidden You don't have permission to access / on this server

All I wanted to do today was to write a redirect rule to a subfolder, e.g.: You enter the URL: example.com and you get redirected to example.com/subfolder Such a simple wish. I tried to find a solution on the internet. The internet told me to add an…
QuantumHive
  • 5,613
  • 4
  • 33
  • 55
64
votes
3 answers

What does RewriteBase do and how to use it?

I am trying to learn some .htaccess tricks. I came across the RewriteBase directive but could not get it to work properly. I wonder what this directive does specifically and how to use it. There have been some discussions about RewriteBase in…
Chong Lip Phang
  • 8,755
  • 5
  • 65
  • 100
60
votes
1 answer

Rewrite rule if condition is not matched

How may I perform a rule if the URL is NOT matching the path "forums"? For example: RewriteCond IF URL IS NOT forums RewriteRule !\.(js|gif|css|jpg|png)$ %{DOCUMENT_ROOT}/index.php [L]
jcoahd
  • 713
  • 2
  • 6
  • 7
57
votes
2 answers

apache HTTP:X-Forwarded-Proto in .htaccess is causing redirect loop in dev environment

I've had to update my .htaccess from this: RewriteCond %{HTTPS} !=on RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] to this: RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] to get…
greg
  • 6,853
  • 15
  • 58
  • 71
54
votes
4 answers

Multiple RewriteRules for single RewriteCond in .htaccess

I have following command in my .htaccess RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.example\.com [NC] RewriteRule ^(.*?)-([a-z]+) %2/$1.$2 [L] RewriteRule ^(.*?)-([0-9]+)([a-z]) %2/$1$3.$2 [L] %2 is not working in second and later…
Huseyin
  • 1,499
  • 2
  • 25
  • 39
54
votes
11 answers

htaccess remove index.php from url

I have a problem whereby google has indexed some pages with the wrong url. The url they are indexing is: http://www.example.com/index.php/section1/section2 I need it to redirect to: http://www.example.com/section1/section2 .htaccess isn't my…
Nick
  • 715
  • 2
  • 7
  • 10
54
votes
4 answers

Permission denied: /var/www/abc/.htaccess pcfg_openfile: unable to check htaccess file, ensure it is readable?

Hi all I'm using PHP for my website and ubuntu linux on my system. I got the above error in error.log file of apache, even after configurating everything properly. I did a lot of research on this but couldn't be able to resolve the issue. Can anyone…
PHPLover
  • 1
  • 51
  • 158
  • 311
53
votes
1 answer

mod_rewrite urlencoding an already urlencoded query string parameter - any way to disable this?

We have some very basic mod_rewrite rules: RewriteRule ^something.swf$ http://www.newdomain.com/something.swf [R=302,L] mod_rewrite seems to do its job, and if the user is accessing: something.swf?param=value, it is redirected correctly to…
anonymous-one
  • 14,454
  • 18
  • 60
  • 84
52
votes
5 answers

How can I use an .htaccess file in Nginx?

I am currently migrating my website from Apache to nginx, but my .htaccess file is not working. My website is inside the /usr/share/nginx/html/mywebsite folder. How can I use .htaccess in my nginx server? This is my .htaccess file: RewriteEngine…
Sandeep Bhaskaran
  • 621
  • 1
  • 7
  • 14
50
votes
9 answers

Removing the .php extension with mod_rewrite

I want a mod_rewrite rule set, so I can refer to a page without the .php extension, but have that rewritten to include the .php extension. This will be running on a 1&1 server. Are there any good references so I can learn more myself?
James
  • 1,764
  • 5
  • 31
  • 49
50
votes
8 answers

How do you enable mod_rewrite on any OS?

If I understand correctly, I need to put something in httpd.config to enable mod_rewrite. If this is true, what do I need to put in httpd.conf or apache.conf? Please be OS specific.
John
  • 4,820
  • 21
  • 62
  • 92
49
votes
1 answer

How can I match query string variables with mod_rewrite?

Suppose I have URLs with query string parameters like these: /index.php?book=DesignPatterns&page=139 /index.php?book=Refactoring&page=285 Using mod_rewrite, how can I redirect them to SES URLs like these? /DesignPatterns/139 /Refactoring/285
Patrick McElhaney
  • 57,901
  • 40
  • 134
  • 167