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
35
votes
12 answers

404 Not Found The requested URL was not found on this server

I'm having small troubles and was wondering if someone can help me over the hump. I pulled a copy of a website down from Hostgator and I'm trying to set it up on my local machine using WAMP but, I keep getting an error when trying to access the…
SkillSet
  • 621
  • 2
  • 7
  • 15
34
votes
3 answers

htaccess redirect index.php to root (including subdomains)

I'm trying to redirect index.php files to the root /, I've searched around and found several snippets of code similar to: RewriteCond %{THE_REQUEST} ^.*/index.php RewriteRule ^(.*)index.php$ http://www.domain.com/$1 [R=301,L] This however won't…
Alex
  • 1,018
  • 1
  • 11
  • 21
33
votes
5 answers

Preserve HTTP/HTTPS protocol in .htaccess redirects

I have to redirect port 80 to 2368 in htaccess but I'd like to keep the requested protocol intact so that SSL doesn't break. I currently have this: RewriteCond %{HTTP_HOST} ^sub.domain.com$ [NC] RewriteRule ^ http://sub.domain.com:2368%{REQUEST_URI}…
secondman
  • 3,233
  • 6
  • 43
  • 66
33
votes
4 answers

Force SSL/HTTPS with mod_rewrite

I have a Zend Framework application that I want to force into HTTPS using mod_rewrite. I am pretty lost when it comes to mod_rewrite. Here is my current .htaccess file in the root of my application. RewriteEngine on RewriteCond %{REQUEST_FILENAME}…
RSN
32
votes
3 answers

.htaccess url-rewrite if file not exists

I must do a little trick for a site! The idea is: if a file for a required url exists then I go to that url, doing nothing more; if a file for a required url not exists, I must go to a file.php and than do something, but NOT changing the…
Kiavor
  • 476
  • 1
  • 7
  • 14
32
votes
3 answers

Apache rewrite based on subdomain

I'm trying to redirect requests for a wildcard domain to a sub-directory. ie. something.blah.example.com --> blah.example.com/something I don't know how to get the subdomain name to use in the rewrite rule. Final Solution: RewriteCond %{HTTP_HOST}…
Sam
  • 5,416
  • 7
  • 47
  • 49
32
votes
2 answers

Redirect to other domain but keep typed domain

After looking on the internet for about an hour, I didn't find the answer to my question. So I'm searching with the wrong keywords or what I want is not possible. What I want: I have multiple domains with different extensions, for…
Timo002
  • 3,138
  • 4
  • 40
  • 65
31
votes
5 answers

How to encode special characters using mod_rewrite & Apache?

I would like to have pretty URLs for my tagging system along with all the special characters: +, &, #, %, and =. Is there a way to do this with mod_rewrite without having to double encode the links? I notice that delicious.com and stackoverflow…
Aldie
  • 819
  • 1
  • 11
  • 16
30
votes
16 answers

.htaccess not working (mod_rewrite)

I have not having any luck getting my .htaccess with mod_rewrite working. Basically all I am trying to do is remove 'www' from "http://www.example.com" and "https://www.example.com". If there is anything I am missing (conf files, etc let me know I…
Kladskull
  • 10,332
  • 20
  • 69
  • 111
30
votes
8 answers

.htaccess rewrite GET variables

I have a index.php which handle all the routing index.php?page=controller (simplified) just to split up the logic with the view. Options +FollowSymlinks RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME}…
John
  • 2,900
  • 8
  • 36
  • 65
30
votes
7 answers

500 Internal Server Error when using .htaccess with RewriteEngine

I am on the shared host Bounceweb and I am trying to add some rewrite rules to make my links look prettier. One of these rules is to make the url: http://mysite.com/upload point to: http://mysite.com/upload.php. I have this in my .htaccess…
Josh
  • 1,361
  • 5
  • 22
  • 33
30
votes
7 answers

Yii2 htaccess - How to hide frontend/web and backend/web COMPLETELY

I think I am pretty close. I have the htaccess redirecting to the website (frontend/web) and the /admin path (backend/web). The site appears fine, CSS files loading, etc. If you go to: http://localhost/yii2app/ - it loads the homepage, and doesn't…
Wade
  • 3,757
  • 2
  • 32
  • 51
30
votes
4 answers

Is there a way to force apache to return 404 instead of 403?

Is there a way how I can configure the Apache web server to return a 404 (not found) error code instead of 403 (forbidden) for some specific directories which I want to disallow to be accessed? I found some solutions suggesting the use of…
29
votes
3 answers

Multiple RewriteConditions: How to chain them before a set of rules?

Q1: How to chain these two conditions making them if BOTH A AND B, then proceed... Q2: How to make them stick for all the rewriteRules below and not just the first rule? RewriteCond %{REQUEST_URI} ^IMAGE-.*$ // if filename starts with IMG-…
Sam
  • 15,254
  • 25
  • 90
  • 145
29
votes
2 answers

What does this this HTTP Authorization RewriteRule do?

I have an rewrite recursion error somewhere on my website that Google Bot caused, but I can't find the url that caused it because my Loglevel is low. I raised it but it has not happened again so far. RewriteRule .* -…
The Surrican
  • 29,118
  • 24
  • 122
  • 168