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

how to remove folder name from url using htaccess

I want to change the URL from: http://example.com/Portfolios/iPhone/app To: http://example.com/iPhone/app And same for all URLs like: example.com/Portfolios/iPad/app To: example.com/iPad/app And…
anytime
  • 409
  • 3
  • 7
  • 14
23
votes
3 answers

.htaccess RewriteRule to path without changing URL

So, I've this problem: Base Website located at http://example.com/ Second Website located at http://example.com/web2/ People making various requests to the second website like http://example.com/myWeb/pg1 and http://example.com/web2/pg2 Recently…
TCB13
  • 3,067
  • 2
  • 39
  • 68
23
votes
4 answers

How do I make URLs case insensitive in Linux server

I am working a website which is deployed on a Linux server. I have small changes to do on that. I have folder read. The requirement is that if I enter the URL localhost:80/tom/Read or ../READ or /read it needs to navigate to read.php inside a read…
sonorita
  • 771
  • 2
  • 8
  • 20
23
votes
2 answers

nginx redirect all directories except one

I'm using nginx 1.0.8 and I'm trying to redirect all visitors from www.mysite.com/dir to google search page http://www.google.com/search?q=dir where dir is a variable, however if dir=="blog"( www.mysite.com/blog) I just want to load the blog…
Doua Beri
  • 10,612
  • 18
  • 89
  • 138
22
votes
1 answer

Apache rewrite rule for a destination containing a hash mark

I'm trying to issue a redirect where the destination contains a fragment-identifier part. I tried with this rule: RewriteRule ^/foo/bar/([^/]+)/(.*)$ /cgi/script#foobar::$1.$2 [R,L] However the # is converted into %23 and the web application…
ascobol
  • 7,554
  • 7
  • 49
  • 70
22
votes
2 answers

.htaccess or symbolic link (symlink)

I have a website with multiple folders and I was trying to fix them in my .htaccess. After a little while, I have a big .htaccess with rules that conflicts. Now every time I want to add a folder I have to add it to the .htaccess. I did some research…
Adam
  • 1,205
  • 10
  • 20
22
votes
13 answers

CSS not loading after redirect with htaccess rewrite rule

I have the following Short-hand for a user profile url RewriteRule ^(\w+)/?$ ./profile.php?name_of_user=$1 The site is styled with the appropriate css file when i do site.com/name_of_user but not when i do site.com/name_of_user/ Although the…
algorithmicCoder
  • 6,595
  • 20
  • 68
  • 117
22
votes
4 answers

.htaccess: GET variables are lost in rewrite

Apparently, my .htaccess rewrite eats up all $_GET-variables on my page: When accessing the URL http://192.168.1.1/welcome/test?getvar=true and running var_dump($_GET) in my index.php file, I get this this output: array '/welcome/test' => string ''…
Industrial
  • 41,400
  • 69
  • 194
  • 289
22
votes
2 answers

How to Simulate & Test URL rewrite rule in Apache 2, when on sharehosting RewriteLog is disabled/unallowed in .Htaccess?

How to Simulate & Check a URL rewrite rule in Apache 2, when on sharehosting RewriteLog is disabled and or NOT allowed to be set in .Htaccess? In other words, are there any tools/simulators to test an url rewrite independantly of hosting, to check…
Sam
  • 15,254
  • 25
  • 90
  • 145
22
votes
2 answers

Apache RewriteRule, - (dash) as substitution

I often see in PHP MVC applications an Apache RewriteRule that looks like this: RewriteRule ^.*$ - [NC,L] The Apache docs for the RewriteRule directive states: A dash indicates that no substitution should be performed (the existing path is passed…
Cobby
  • 5,273
  • 4
  • 28
  • 41
22
votes
5 answers

mod_rewrite and double slash issue

I applied the following mod_rewrite rule in Apache2 to redirect from non-www to www: RewriteEngine On RewriteCond %{HTTP_HOST} ^example\.com$ [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] However, there's a double slash issue: When I…
Mark
  • 67,098
  • 47
  • 117
  • 162
22
votes
8 answers

How can I implement a global RewriteCond / RewriteRule in Apache that applies to all virtual hosts?

The title pretty much says it all. :-) I have lots of virtual hosts and I want to put a single rewriting block at the top of the httpd.conf file that rewrites URLs no matter which virtual host the request might be directed to. How the heck do I…
hourback
  • 1,150
  • 4
  • 12
  • 27
22
votes
3 answers

.htaccess directives to *not* redirect certain URLs

In an application that heavily relies on .htaccess RewriteRules for its PrettyURLs (CakePHP in my case), how do I correctly set up directives to exclude certain directories from this rewriting? That is: /appRoot/.htaccess app/ …
deceze
  • 510,633
  • 85
  • 743
  • 889
22
votes
1 answer

With mod_rewrite can I specify a RewriteBase within a RewriteCond?

I'd like to have my .htaccess file specify a different RewriteBase depending on whether the .htaccess file is on my local machine or on a web server. This is the mod_rewrite code I tried, however, it did not work: RewriteCond %{HTTP_HOST}…
user229448
22
votes
5 answers

How to host static content pre-compressed in apache?

I host a JavaScript game which basically consists of an .html and a .data file. If I compress them with gzip, their size shrinks to 25%. So I want to do that. I'm not 100% sure, but I think using mod_gzip or mod_deflate does the compression…
marc40000
  • 3,167
  • 9
  • 41
  • 63