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

How do I make .htaccess work on the current directory and not subdirectories?

I'm using mod rewrite to access my PHP files in the root directory of my website indirectly. Currently, I have something like this, RewriteRule ^(blog|about|page3|etc)$ /$1.php [L] But what I would like to use is RewriteRule ^(.*)$ /$1.php [L] So…
fent
  • 17,861
  • 15
  • 87
  • 91
4
votes
1 answer

Strange behavior in apache mod_rewrite

I have a problem with understanding mod_rewrite behavior. I'll illustrate this with an example. I have 3 files in my root directory: .htaccess, index.php and test.php. The content of files: .htaccess RewriteEngine On RewriteBase / RewriteCond…
Kirill Smirnov
  • 1,452
  • 2
  • 21
  • 28
4
votes
2 answers

Apache Rewrite or proxy

I have an Apache server running on my machine (port 80) I have a Zope server running on my machine (port 8080) i want all users, irrespective of domain (lets use www.example.com for now) to be pushed to the zope instance, seamlessly IE if i type…
DrogoNevets
  • 1,456
  • 3
  • 18
  • 34
4
votes
1 answer

rewrite/redirect specific word from url .htaccess

I have couple of urls which is generated by wordpress/php http://mydomain.com/news-2/newsarticle http://mydomain.com/products/category-2 http://mydomain.com/products/category/products-2 how can I rewrite/redirect any url with -2 to the one…
MonteCristo
  • 1,471
  • 2
  • 20
  • 41
4
votes
1 answer

.htaccess url rewrite for ssl https redirection

I've used different codes provided here on other questions' solutions and on the internet. I'm really not savvy with htaccess. Bought and confirmed working SSL Certificate, but I'm new to applying these redirects. Goal: I need to rewrite http to…
crowflux
  • 55
  • 1
  • 5
4
votes
1 answer

mod_rewrite to nginx rewrite rules

I have converted most of my Apache HTTPd mod_rewrite rules over to nginx's HttpRewrite module (which calls PHP-FPM via FastCGI on every dynamic request). Simple rules which are defined by hard locations work fine: location = /favicon.ico { rewrite…
abestic9
  • 491
  • 5
  • 19
4
votes
2 answers

.htaccess: Rewrite to folder except if cookie exists or before a date bypassing WordPress rewrite

I find myself migrating a static site to a WordPress-based website fairly frequently these days. Once I've finished development of the WordPress website on a test server, I then want to install it on the current website domain, but without affecting…
iagdotme
  • 1,033
  • 2
  • 21
  • 38
4
votes
1 answer

Using .htaccess to change what URL is being displayed, but without actually redirecting the content?

I have a RewriteRule setup to change http://kn3rdmeister.com/blog/post.php?y=2012&m=07&d=04&id=4 into http://kn3rdmeister.com/blog/2012/07/04/4.php but that actually redirects where the browser is getting the page from. I want to still display…
mcneiljm
  • 77
  • 1
  • 8
4
votes
0 answers

Apache Content Negotiation, File Extensions, and Permanent Redirects

Given an Apache 2.x web server that uses Content Negotiation (+MultiViews) to access URLs without their extensions (e.g., allow /foo vs. /foo.php or /foo.html), how does one issue a 301 permanent redirect when someone does in fact try to use those…
Joe D'Andrea
  • 5,141
  • 6
  • 49
  • 67
4
votes
1 answer

htaccess for clean urls not being read by apache?

I'm getting kinda crazy over here because I already wasted two hours getting my clean URLs enabled for the laravel framework. I'm on a Mac and have a XAMPP setup. The Loadmodule mod_rewrite is commented out and php_info() says mod_rewrite module is…
Boyd
  • 723
  • 4
  • 15
  • 32
4
votes
1 answer

mod_rewrite in a subdirectory

Our web server is structured as follows /path-to-docdir [Contains main pages] ./app/ [Some application not directly related to the main content] The main pages are served fine. In the app directory I am trying to use mod_rewrite…
sumansudha
  • 43
  • 1
  • 6
4
votes
3 answers

Disabling TRACE request method on Apache/2.0.52

By default, Apache 2.0.52 will respond to any HTTP TRACE request that it receives. This is a potential security problem because it can allow certain types of XSS attacks. For details, see http://www.apacheweek.com/issues/03-01-24#news I am trying to…
GDere
4
votes
1 answer

Apache with kerberos + mod_proxy + mod_rewrite recursion

I've configured an Apache server to provide SSO and reverse-proxying for our Splunk installation as per the config below. SSO works as does the reverse-proxy hiding the Splunk instance running on port 8000 behind the /splunk URL. ProxyPass /splunk…
Mark S
  • 47
  • 2
  • 7
4
votes
2 answers

how to pass more than 9 parameters in the file. htaccess (mod_rewrite)?

I'm having trouble spending more than nine parameters in. htaccess file with mod_rewrite apache, for example, if I pass the parameter $ 10 = "something" mod_rewrite passes the value of parameter $ 1. Is there any solution for this?
Felipe
  • 63
  • 7
4
votes
3 answers

Can I re-write my URLs like this and is it a good idea?

Is it possible to re-write (Apache Mod-Rewrite) a URL from this: http://www.example.com/view.php?t=h5k6 to this http://www.example.com/h5k6 The reason for this re-write is that the URL needs to be very short (a bit like a tiny URL service). Would…
Abs
  • 56,052
  • 101
  • 275
  • 409