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

.htaccess in subdirectory 'overriding' parent htaccess

been searching for 2 days and can't quite get the right solution due to my lack of understanding of mod_rewrite and time constraints on this project so hoping someone can help. The aim To rewrite all requests to the root index.php if the client…
Abe
  • 183
  • 1
  • 1
  • 4
18
votes
4 answers

PHP: How can I get the URL that has been rewritten with mod_rewrite?

For example, if I rewrite /category/topic/post/ to /index.php?cat=1&topic=2&post=3, how can I get /index.php?cat=1&topic=2&post=3 using PHP?
Linksku
  • 1,449
  • 6
  • 17
  • 22
18
votes
2 answers

.htaccess rewrite pass all query strings through

Currently I use: RewriteRule ^([^/\.]+)/?$ page.php?page=$1 [L] Which is good for when passing one querystring through. But is there a way just to pass all querystrings through? i.e. One page request might be: domain.com/contact?course=23 and…
Shane
  • 1,603
  • 4
  • 26
  • 54
18
votes
2 answers

.htaccess rewrite with WordPress plugin

I am working on building a WordPress plugin for a custom WordPress multisite network and have a few files that use URL variables to load information from a second database (not the WordPress database). In the version built outside of WordPress by…
Dustin Snider
  • 678
  • 1
  • 8
  • 29
18
votes
6 answers

Letsencrypt with htaccess

This is my current htaccess configuration of /frontend/web RewriteEngine on RewriteCond %{HTTPS} !=on RewriteRule ^.*$ https://%{SERVER_NAME} [R,L] # if a directory or a file exists, use it directly RewriteCond %{REQUEST_FILENAME} !-f RewriteCond…
revengezp
  • 181
  • 1
  • 1
  • 3
18
votes
4 answers

.htaccess - "Too many redirects" when trying to force https

I am trying to force a subfolder (/bbb/) of my root domain to show always as https. Also my .htaccess file take care of the extensions of the pages. I have put the .htaccess file inside my /bbb/ folder but I get "Too many redirects" when I try to…
SNos
  • 3,430
  • 5
  • 42
  • 92
18
votes
13 answers

How to redirect http to https in codeigniter

Point #1 If I type: www.myurl.com/somepage http://www.myurl.com/somepage http://myurl.com/somepage https://myurl.com/somepage have it redirect to https://www.myurl.com/somepage Point #2 When I type in something like www.myurl.com it is redirecting…
asiya khan
  • 311
  • 1
  • 2
  • 8
18
votes
4 answers

Force HTTPS on certain URLs and force HTTP for all others

I have a client project where I need to force HTTPS for a certain folder and force HTTP for all others. I can sucessfully enforce HTTPS for the folder I desire but then all links back to the rest of the site end up being through HTTPS. I'd like to…
Nathan Pitman
  • 2,286
  • 4
  • 30
  • 46
18
votes
7 answers

How to enable mod_rewrite on Apache 2.4?

I'm using Wordpress and wanted to enable pretty urls feature, but they don't work, i'm thinking that it's because of mod_rewrite, but i don't know how to enable it on Apache 2.4 under CentOS 7... I've already try this: grep -i LoadModule…
Jonathan Solorzano
  • 6,812
  • 20
  • 70
  • 131
18
votes
3 answers

Use Mod_Rewrite to Rewrite ROOT to Another Path

Note: I have tried the suggestions under this question but they either don't work or give me an infinite redirect loop error. I have the following in my .htaccess file: Options +FollowSymLinks RewriteEngine On …
leek
  • 11,803
  • 8
  • 45
  • 61
18
votes
1 answer

Forcing a specific page to use HTTPS with angularjs

In our application we have a payment page that we want to use SSL on because we are handling credit card information. We've already put in place rewrite rules for apache to redirect a request to the specific page to HTTPS -- which takes care of any…
Matt Foxx Duncan
  • 2,074
  • 3
  • 23
  • 38
18
votes
1 answer

Trying to put an exception to RewriteRule in .htaccess

I am redirecting all requests like so: RewriteRule ^sitemap.xml$ sitemap.php?/ [QSA,L] # the line below is the one I'm having trouble with RewriteCond %{REQUEST_URI} !^market-reports$ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond…
alex
  • 479,566
  • 201
  • 878
  • 984
18
votes
5 answers

Internal URL rewrite no longer working after upgrading Apache to 2.4

I need to internally rewrite the base URL so that instead of / being mapped to /index.php (default behaviour) it goes to another script named contents.php. I have a simple .htaccess in the doc root: RewriteEngine on RewriteRule ^/?$…
LexLythius
  • 1,904
  • 1
  • 12
  • 20
18
votes
2 answers

Reference capture groups of multiple RewriteCond in RewriteRule

When I have multiple RewriteCond chained together, only the capture groups of the last RewriteCond can be referenced with %0-%9. In the following problem the parameters in the query string of the url can be in any order. To parse them into a fancy…
Sumurai8
  • 20,333
  • 11
  • 66
  • 100
18
votes
1 answer

%N backreference inside RewriteCond

I'm working on a virtual domain system. I have a wildcard DNS set up as *.loc, and I'm trying to work on my .htaccess file. The following code works: RewriteEngine On RewriteCond %{HTTP_HOST} ^(www.)?example\.loc$ [NC] RewriteCond %{REQUEST_URI}…