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

Can mod_rewrite find a variable within an URL, replace a portion before it, delete the variable and preserve what remains?

I've looked around but frankly I am not sure of the lingo I need to be searching with to get more exacting results. Heck, I am struggling with url vs. uri vs. page vs. link and what each is and isn't. And the MOD_REWRITE guides seem to be written…
4
votes
2 answers

is the value of RewriteBase available as a variable/reference?

I'm writing an .htaccess file that will check if a requested page exists in a cache or not. In order to perform the check (and save typing), I'm setting an ENV variable with the location of the cache: # all this works as I expect #
traq
  • 281
  • 2
  • 11
4
votes
1 answer

mod_rewrite apache for custom flat links gets stuck

I want to create flat links for my website. The .htaccess code is following: RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule…
Sukanta Paul
  • 784
  • 3
  • 19
  • 36
4
votes
4 answers

How can I redirect all URLs containing an IP address to my www domain?

I have several servers with different IP addresses. I want to make it impossible to reach them via IP directly. Instead, I want them to redirect to my domain, www.site.com. The following condition matches an exact IP: RewriteCond %{REMOTE_HOST}…
ty.
  • 10,924
  • 9
  • 52
  • 71
4
votes
1 answer

Rewrite rule for httpd.conf and virtual host SSL

I have two virtual hosts in httpd.conf one for port 443 and one for port 80: Now I want to redirect every request to my server to go to https://www.mysite.com/…
Tamik Soziev
  • 14,307
  • 5
  • 43
  • 55
4
votes
3 answers

How does .htaccess work?

I'm trying to make my website display the other pages as a www.example.com/pageone/ link instead of www.example.com/pageone.html. Problem is, i'm reading up on ways to do that using .htaccess and its getting me confused because i don't understand…
Kyle Yeo
  • 2,270
  • 5
  • 31
  • 53
4
votes
2 answers

symfony2 on shared hosting

I've been working on this for quite a while to no avail.. I've got symfony installed on a shared web-host where I have zero access to change the DocumentRoot to the web/ folder so that is not an option to me... I've written my…
Justin
  • 2,131
  • 4
  • 24
  • 41
4
votes
5 answers

(htaccess) How to make a file only accessable by host Server and prevent direct access?

I'm using Apache and lets say there is a file at "images/sample.jpg". With .htaccess, how can i make it appearing on the website and then prevent the direct access by url (for example, direct url like "http://www.abc.com/images/sample.jpg") ? If…
夏期劇場
  • 17,821
  • 44
  • 135
  • 217
4
votes
3 answers

How do I rewrite a subset of urls (/api/something)?

I have a .htaccess rule that basically points all access through one page, and I have PHP handling it from that point. So, no matter what directory it is in, it'll pass all URLs to this script. If my root was /project/ (…
Ben Dauphinee
  • 4,061
  • 8
  • 40
  • 59
4
votes
2 answers

Static resources being incorrectly rewritten to front-controller index.php

I've got a problem with rewriting urls. The following is happening: http://www.example.com/scores http://www.example.com/registreren http://www.example.com/login these urls will be redirected to index.php?route=scores etc This is all working very…
4
votes
1 answer

multiple mod_rewrite rules in .htaccess

I am having difficulty getting several mod_rewrite rules to work together in my .htaccess files. Throughout the enitre site I want to drop the "www." from all URLs. I am using the following at the document root: Options +FollowSymLinks RewriteCond…
Andy B
  • 205
  • 4
  • 9
4
votes
1 answer

(Un)setting a cookie in mod_rewrite

I'd like to remove a cookie using mod_rewrite but unfortunately the subsequent request (Chrome,FF,Safari) keeps the old cookie value. Doku ( http://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_co )…
pagid
  • 13,559
  • 11
  • 78
  • 104
3
votes
2 answers

.htaccess mod_rewrite won't skip RewriteRule with [S]

As an FYI, I am using the following .htaccess file in located at www.site.com/content/ When a user visits www.site.com/content/login I want it to display the content from www.site.com/content/userlogin.php (masked via rewrite, and not redirect) -…
iverSUN
  • 35
  • 7
3
votes
4 answers

Using .htaccess to rewrite a simple url

I have a url like this - www.example.com/blog.php/username, it will take you to the username's blog page, but I want to use a url like this instead - www.example.com/blog/username to get to the same file (blog.php). Please what are the steps I need…
Chibuzo
  • 6,112
  • 3
  • 29
  • 51
3
votes
2 answers

htaccess rewrite pass two variables or one depending if both are available?

I want to pass first directory as a variable and subdirectory as another variable. It works when for urls with both directories (/something/something2) but I get an error 404 when I try with only one (/something). RewriteRule ([^/]+)/([^/]+)…
lisovaccaro
  • 32,502
  • 98
  • 258
  • 410