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

mod_rewrite to alias one file suffix type to another

I hope I can explain this clearly enough, but if not let me know and I'll try to clarify. I'm currently developing a site using ColdFusion and have a mod_rewrite rule in place to make it look like the site is using PHP. Any requests for index.php…
James Marshall
  • 630
  • 2
  • 7
  • 15
3
votes
2 answers

htaccess mod_rewrite .tld to specific folder

I want to do the following, if possible, with htaccess. The webpage works this way, you get the index file via domain.de/de/start and the english version via domain.de/en/start. If a user visits domain.com I would like him to end up at…
Lukas Oppermann
  • 2,918
  • 6
  • 47
  • 62
3
votes
3 answers

How to write a wrapper - avoiding a subshell - script for apache2 rewrite maps or shell scripts in general?

I am running a rewrite map with an external rewrite program (prg) in apache2 that may produce an error and die. When the rewrite map is not running any more the system obviously doesn't function properly. So I wanted to start a simple wrapper shell…
The Surrican
  • 29,118
  • 24
  • 122
  • 168
3
votes
3 answers

301 Redirecting URLs based on GET variables in .htaccess

I have a few messy old URLs like... http://www.example.com/bunch.of/unneeded/crap?opendocument&part=1 http://www.example.com/bunch.of/unneeded/crap?opendocument&part=2 ...that I want to redirect to the newer, cleaner…
user84643
  • 379
  • 2
  • 5
  • 15
3
votes
1 answer

Apache rewrite all urls except those beginning with certain paths

I'm trying to redirect all requests except those beginning with certain paths. for example, http://www.example.com/mypath1 shouldn't redirect http://www.example.com/mypath2 shouldn't redirect Everything else should redirect So far ive tried getting…
cowls
  • 24,013
  • 8
  • 48
  • 78
3
votes
1 answer

Rewrite: Leading dot in the cookie domain

We have a web application that is using a cookie to store a code. The domain of the cookie is 'www.mydomain.com'. We've made rewrite rules to create the cookie for customers entering on spesific urls: RewriteRule ^/example.*…
Hjallmar
  • 31
  • 2
3
votes
1 answer

How to replace dashes with slashes in .htaccess

I want to replace all the dashes from my PHP filenames but I don't know how to do it. Basically I have something like this: http://localhost/category-activity.php And I want to end up with this: http://localhost/category/activity I also need that…
Agustin
  • 31
  • 2
3
votes
2 answers

Apache Rewrite to Nginx Rewrite

I have the following apache rewrite rule: RewriteEngine On RewriteRule ^id/([^/\.]+)$ item.php?id=$1 [L] and I need to be able to use it with Nginx. It is meant to so that someone can go to http://mysite.com/id/10 and instead of having to be go…
user1104791
3
votes
1 answer

Mod rewrite + hash tags

Does mod rewrite work together with hashtags? Maybe my code is crap, but this does not seem to work: My site uses this hash tag: index.php/#p/about It works fine using regular hashtags but i want it to work with mod rewrite so i tried…
Richard
  • 14,427
  • 9
  • 57
  • 85
3
votes
2 answers

htaccess and removing sub-domain from HTTP_HOST

I've got a wildcard DNS record, allowing all forms of *.domain.com, additionally I'm pointing several different domains at the same machine and using rewrite rules to direct requests to sub folders depending upon the url the request originates. For…
Jackson
  • 31
  • 3
3
votes
2 answers

Redirect Subdomain to new domain

Hi guys trying to get a 301 redirect working and having trouble. I need to redirect sub.domain1.com to www.domain2.com and make sure that any file names or parameters get sent over with it. This is what I was trying: RewriteCond %{HTTP_HOST}…
spyke01
  • 435
  • 3
  • 15
3
votes
3 answers

.php url to .html url

I have a website built in .php but we have converted it to .html by using "mod rewrite". the mod rewrite code used in .htaccess is RewriteEngine on RewriteRule ^(.*)\.html$ $1.php Now the problem is my website shows up in both .php and .html. for…
sammrat23
  • 31
  • 1
  • 2
3
votes
1 answer

301 Redirects (htaccess) with exclude conditions

I'm currently moving a site (shop) to a new domain and putting in its place a non-shop version (made in wordpress) of the site. The aim is for most of the urls to redirect to the new domain except the few pages that the new site has. I've found…
dciso
  • 1,264
  • 3
  • 18
  • 31
3
votes
1 answer

How to change RewriteBase dynamically based on hostname or IP address?

Currently I'm building application on my local computer. Application is located in /www/projects/mycustomproject/ and for speed of access it has its own personal domain, lets say mycustomproject.com. I have made my Apache-PHP-MySQL available via…
Nazariy
  • 6,028
  • 5
  • 37
  • 61
3
votes
0 answers

Apache mod_rewrite serves same content for different urls

I have strange issue, I cant find the answer nowhere. I have set apache2 server with mod_disk_cache. I have strange issues where I get same page for different URL's. Let's say that I visit page via homepage - it works fine. If I go to the article,…
DarkSideOfTheMoon83
  • 702
  • 1
  • 8
  • 17