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
1 answer

how do I skip a RewriteRule if REQUEST_URI is a specific directory. This is a CodeIgniter project

I have a CodeIgniter project running on CentOS, with an EV cert successfully installed. I don't want SSL used when the /rss directory is accessed. I had this working, but I broke it somehow during an upgrade. Here is my .htaccess file, which I think…
wclark
  • 436
  • 4
  • 14
3
votes
4 answers

Repeatedly appended URLs after RewriteRule in .htaccess?

This is my first time re-writing urls to be SEO friendly. Here's my htaccess rule: RewriteEngine On RewriteRule ratings/ ratings.php RewriteRule regions/ regionlist.php RewriteRule mobile/ mobile.php For some reason, when I click the same link…
Joel Murphy
  • 2,472
  • 3
  • 29
  • 47
3
votes
2 answers

htaccess RewriteCond, Multiple Conditions for RewriteRule

I use .htaccess Mod Rewrite to send the URL to index.php, which then parses the URL and builds each page of my website. This works perfectly fine, allowing me to easily manage clean URLs using PHP instead of .htaccess. Ex:…
Jay
  • 1,084
  • 4
  • 18
  • 43
3
votes
1 answer

mod_rewrite: the precedence of [OR] and implicit [AND]

Is it possible to check for X and (Y or Z) with mod_rewrite? I have the following rule to serve dummy.png to guests of my website, who don't have 2 cookies id and auth set (I set those in my custom Drupal module for registered users): RewriteCond…
Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
3
votes
1 answer

.htaccess url rewrite with variables

I'm trying to make my urls look prettier in a specific subdirectory on my site by using rewrite to change: www.example.com/sub/file/{some_number}/ internally to: www.example.com/sub/file.php?var={some_number} And I keep getting a 404. I'm already…
chris
  • 2,893
  • 3
  • 20
  • 22
3
votes
2 answers

How would I write an if - elseif - elseif conditional block in an .htaccess file?

This is what I want to accomplish in pseudocode: if server.host equals 'productionsite.com' then RewriteBase / else if server.host equals 'stagingsite.com' then RewriteBase /staging/mysite else if server.host equals 'localhost' or server.host…
racl101
  • 3,760
  • 4
  • 34
  • 33
3
votes
3 answers

How do i do readable urls?

Assume I have the url http://example.com/user/me, where me is the user name. When the user types the url into the address bar, I want to reveal the details of the user. I do not want urls such as http://example.com/user.php?user=me Any help…
Pete
3
votes
1 answer

Rewriting query URL

Suppose I have URLs with query string parameters like these: index.php?lag=en&name=About Us&itemid=60 index.php?host=consumer&lag=en&name=About Us&itemid=64 Using mod_rewrite, how can I redirect them like these? en_About Us_60.php consumer/en_About…
pradeep
  • 56
  • 4
3
votes
1 answer

404 Image Placeholder

I've got a product gallery I'm working on and just did an import. This added a bunch of references to images that don't exist on the server. Right now when the image tries to load, it's got that broken image icon, which is ugly, and well, should be…
WNRosenberg
  • 1,862
  • 5
  • 22
  • 31
3
votes
1 answer

htaccess regexp Underline and Space doesn't work

RewriteRule ^([A-Za-z0-9'"%ãõáéíóúâêîôûàÁÃÕÁÉÍÓÚÂÊÎÔÛÀ\/\.\-]*)$ public_html/$1 [NC] I'm using this Regex up here and it works just fine, but if I put for example: RewriteRule ^([A-Za-z0-9 _'"%ãõáéíóúâêîôûàÁÃÕÁÉÍÓÚÂÊÎÔÛÀ\/\.\-]*)$ public_html/$1…
Grego
  • 2,220
  • 9
  • 43
  • 64
3
votes
1 answer

htaccess Rewrite Rule - Allowing Capital Letters in URL

I have the following Rewrite Rule: RewriteEngine On RewriteRule ^brand/([0-9a-zA-Z-]+)/?$ find_by_brand.php?brand=$1 RewriteCond %{THE_REQUEST} find_by_brand\.php RewriteRule ^find_by_brand\.php - [F] I want it to be able to work if I go to…
Drew
  • 6,736
  • 17
  • 64
  • 96
3
votes
1 answer

.htaccess mod_rewrite exemptions

Using a template I have this .htaccess mod_rewrite code that makes my URL's pretty: RewriteEngine On RewriteRule ^posts.php$ posts.php [L] # Rewrite www to no-www domain RewriteCond %{HTTP_HOST} ^www\.(.+)$…
Mike Thrussell
  • 4,175
  • 8
  • 43
  • 59
3
votes
3 answers

Php handling vs Apache RewriteRules and RegExp: which one is quicker?

I've already read this but it doesn't answer to my question. Here's my scenario: I've been working on my own framework which I'm proud of (multilanguage, templating, and so on). But I had to face one problem: the multilanguage and template handling…
Olivier Pons
  • 15,363
  • 26
  • 117
  • 213
3
votes
3 answers

Problems with mod_rewrite and mod_userdir

I'm trying to get a local copy of a website running that someone else put together. The site is deployed and functional and I'm trying to get a development version set up under my public_html directory. The site uses mod_rewrite to redirect all…
Jason George
  • 6,992
  • 8
  • 44
  • 60
3
votes
1 answer

"Spoofing" a 404 not found error with .htaccess

I have .htaccess currently set up to rewrite anything without a period or slash to the equivalent with a .php extension (so "foo" internally pulls up "foo.php") via the following: RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME}…
Doktor J
  • 1,058
  • 1
  • 14
  • 33
1 2 3
99
100