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
22
votes
2 answers

How to redirect URLs based on query string?

I successfully mass migrated a Wordpress site to Drupal. Unfortunately in Wordpress, the content URL's were something like www.example.org/?p=123. My domain is still the same, but I want to do a redirect via htaccess as Drupal will not allow URL's…
user785179
  • 919
  • 5
  • 14
  • 24
22
votes
7 answers

URL rewriting : css, js, and images not loading

I've following rule for .htaccess Options +FollowSymLinks RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteRule ^detail/([0-9]+)/?$ detail.php?id=$1 It redirects http://localhost/detail/123 URL to…
Bhavesh G
  • 3,000
  • 4
  • 39
  • 66
21
votes
4 answers

RewriteEngine on gives error 403

Index file exists and works. When I create .htaccess file with the ONLY line: RewriteEngine On Any page on server gives me: Access forbidden! You don't have permission to access the requested object. It is either read-protected or not readable…
Dmitry
  • 3,861
  • 5
  • 20
  • 21
21
votes
4 answers

Force non-www and https via htaccess

I'm trying to force a user to be redirected to the non-www website, and, force https. I've got this which sort of work, but doesn't force https, when http is entered. RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$…
bear
  • 11,364
  • 26
  • 77
  • 129
21
votes
6 answers

Redirect requests only if the file is not found?

I'm hoping there is a way to do this with mod_rewrite and Apache, but maybe there is another way to consider too. On my site, I have directories set up for re-skinned versions of the site for clients. If the web root is /home/blah/www, a client…
DivideByHero
  • 19,715
  • 24
  • 57
  • 64
21
votes
4 answers

Install Laravel 5 app in subdirectory with htaccess

The Setup I am trying to install a laravel 5 app in this directory on my server: public_html/myapp/ And I want it to be accessed at this URL: http://www.example.com/myapp/ I have created this .htaccess rule and placed it in the myapp folder in order…
Ben Cole
  • 1,847
  • 1
  • 15
  • 18
21
votes
8 answers

Error with .htaccess and mod_rewrite

I'm trying to host a php based application with the following .htaccess values. Options +FollowSymLinks Options -Indexes DirectoryIndex index.php RewriteEngine On RewriteBase /easydeposit RewriteCond %{REQUEST_FILENAME} !-f RewriteCond…
pratz
  • 358
  • 1
  • 3
  • 10
21
votes
4 answers

Add Trailing Slash .htaccess

I'm trying to get the following effect (using this local file http://localhost/[company_name]/[project_name]/.htaccess): http://localhost/[company_name]/[project_name]/page-1 (adds slash) http://localhost/[company_name]/[project_name]/page-1/ (does…
Simon
  • 5,464
  • 6
  • 49
  • 85
20
votes
2 answers

.htaccess with or without slash

What do I need to do to the following rewrite rule to make it so it works whether or not their is a slash at the end of the URL? ie. http://mydomain.com/content/featured or http://mydomain.com/content/featured/ RewriteRule ^content/featured/…
sbuck
  • 1,846
  • 4
  • 24
  • 40
20
votes
5 answers

RewriteCond in .htaccess with negated regex condition doesn't work?

I'm trying to prevent, in this case WordPress, from rewriting certain URLs. In this case I'm trying to prevent it from ever handling a request in the uploads directory, and instead leave those to the server's 404 page. So I'm assuming it's as simple…
nitro2k01
  • 7,627
  • 4
  • 25
  • 30
20
votes
1 answer

how can I define variable in .htaccess file and use it?

I have defined an htaccess file for my website having this code: RewriteEngine on RewriteBase / RewriteRule ^bit_auth\/?(.*)$ /cig/base3/auth/$1 [R=301,NC,L] RewriteCond $1 !^(index\.php|images|robots\.txt|assets|themes|includes) RewriteRule ^(.*)$…
Mojtaba Rezaeian
  • 8,268
  • 8
  • 31
  • 54
20
votes
5 answers

Want to redirect all visitors except for me

Basically I'm about to start work on a site and I'd like something that I can add into my .htaccess file (or elsewhere) that'll work like this pseudo code: (my ip will be in place of 127.0.0.1) if (visitors_ip <> 127.0.0.1) redirectmatch ^(.*)$…
Andrew G. Johnson
  • 26,603
  • 30
  • 91
  • 135
20
votes
5 answers

How to always remove WWW from a url with mod_rewrite?

I'm using the following to try and remove WWW from the url: RewriteCond %{HTTP_HOST} ^example.com$ [NC] RewriteRule (.*) http://example.com$1 [R=301] But for some reason it doesn't work. Any suggestions?
Yeti
  • 5,628
  • 9
  • 45
  • 71
20
votes
7 answers

htaccess works in localhost but doesn't work in EC2 instance

My htaccess file works on localhost but doesn't work when i deploy it to EC2 instance. I'm using Macbook and in finder i cannot see the htaccess file, i thought that perhaps it didn't get copied to EC2 instance but i don't think this is the problem…
20
votes
4 answers

How to remove a cookie in Apache

I need to remove a cookie from the HTTP request that gets to the server. Doing it on the client (that writes this cookie) or on the server (that reads it) is not an option. I have Apache 2.0 that proxies requests between client and the server, so I…
Sergey Golovchenko
  • 18,203
  • 15
  • 55
  • 72