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
3 answers

Symfony2 : redirect localhost/app_dev.php to localhost/

I am developping a website/web-app locally, which includes many ajax calls etc... My problem is everytime I have to include app_dev.php to the url - so when I go on prod I'll need to search and replace these urls.. Is there a way via apache to hide…
George Katsanos
  • 13,524
  • 16
  • 62
  • 98
4
votes
2 answers

mod rewrite not working with laravel and bitNami

http.conf has mod rewrite uncommented so no custom routes are working someone in #laravel mentioned it would be because mod rewrite isn't working here is my setup: laravel.conf has the following code: Alias /laravel/…
arcanine
  • 1,933
  • 1
  • 15
  • 22
4
votes
1 answer

Internal mod_rewrite, no redirection

I would like to make an internal redirect from one URL to another using mod_rewrite in my .htaccess file. Currently I know how to perform the external redirect with the following: RewriteRule ^incoming-controller/action1.*$…
Matt K
  • 6,620
  • 3
  • 38
  • 60
4
votes
2 answers

MOD REWRITE working in local host but not in server

First of all , I clarified with my hosting company and they have verified that Allow Override setting is enabled. Second if I write garbage value in .htaccess file, then I get "Internal Server Error" which further proves that .htaccess is not…
Diana
  • 43
  • 4
4
votes
1 answer

.htaccess mod_rewrite url with multiple optional parameters

I'm pretty new to this mod_rewrite business but I'd like to have a rule that allows me to accomplish the following: localhost/module_name/ -> localhost/index.php?module=module_name localhost/module_name/module_action ->…
Splatbang
  • 756
  • 1
  • 12
  • 29
4
votes
6 answers

How to use apache's mod_rewrite rewriterule without changing relative paths

I've the following rewrite rule in .htaccess: RewriteRule ^groups/([^/\.]+)/?$ groupdetail.php?gname=$1 [L,NC] This takes something like www.example.com/groups/groupname and calls www.example/groupdetail.php?gname=groupname. And it works just…
Suleman Ali
4
votes
0 answers

htaccess - rewritecond to check for slash

I have a rewriterule on my site thats like this: RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ open.php?url=$1 [L] (There is a lot above it but this is the last rule) It works very nicely. What I want to…
4
votes
8 answers

setting up laravel on IIS7

I want to set up my IIS7 server in order to let it works with a web application written in laravel (php framework). I found something similar for CI (link) but it doesn't work on laravel (of course I removed the index.php redirection). actually only…
Dario Rusignuolo
  • 2,090
  • 6
  • 38
  • 68
4
votes
3 answers

Why if I put a "-" dash in a rule in my htaccess doesn't work?

I have the following rules, the one "our-stores" redirects to a different place than the other "PLAIN PAGES" rules: Options +FollowSymlinks RewriteEngine on RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond…
human
  • 686
  • 3
  • 9
  • 24
4
votes
3 answers

mod_rewrite help to change Content-disposition based on URI

I have a directory of mp3 files want to have be able to serve them inline or giving the user an option to download based on the request URI. /media/file1.mp3 -- in this case, I just want to serve the file and let the browser play…
syncopated
  • 63
  • 1
  • 4
4
votes
5 answers

How to redirect users to another domain .htaccess

Server by default redirects users who type example.com/folder to example.com/folder/ Is there any way to change that (for example when they go to example.com/folder to redirect to otherexample.com/folder/). But this has to affect all folders but…
Idrizi.A
  • 9,819
  • 11
  • 47
  • 88
4
votes
1 answer

Redirect .php to extensionless and make url without .php

I dont really know too much about Rewrite in apache, but there is problem with my .htaccess file. the original .htaccess is Options +FollowSymLinks RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME}…
Abhishek
  • 5,649
  • 3
  • 23
  • 42
4
votes
3 answers

The Redirection of Multiple Parked Domains doesn't Work with Filename

My problem seems simple to me but I cannot find a simple solution. Here goes: I have one main domain, and multiple domains pointing to that main domain. To avoid duplicate content I'm trying to redirect all "secondary" or "parked" domains to my main…
4
votes
1 answer

mod_rewrite: can a back reference be referenced twice?

I have a rewrite rule (in an Apache htaccess file) which is attempting to use a back reference twice from just one capture ($1): RewriteRule ^([A-Za-z0-9_-]+)/?$ $1.php?nav=$1 It appears that the query string is being left emtpy,…
dbj44
  • 1,919
  • 2
  • 24
  • 45
4
votes
3 answers

My htaccess RewriteRule works on localhost/mysite but not at mysite.com on a 1&1 shared host

Solved: unfortunately, the solution is not a satisfying one. This morning, when trying @Wige's Suggestion, I found, to my suprise, that the Expected values WERE infact sent to the page as a GET query. Apparently, 1&1 (who I know have been making…
Zach Lysobey
  • 14,959
  • 20
  • 95
  • 149