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
20
votes
6 answers

mod_rewrite: replace underscores with dashes

I'm revealing my embarrassing ignorance of REGEX-fu here, but: I currently have a website where a load of the articles' URLs are written as "article_name", whilst the newer ones are written as "article-name". I want to move all of them to using…
Keith Williams
  • 2,257
  • 3
  • 19
  • 29
20
votes
1 answer

.htaccess redirect - automatically add www. if no subdomain exists

I need any www. added automatically to my domain ONLY IF a subdomain is not already there. I do want subdomains to bypass this redirect. How can I do this?
kdjernigan
  • 417
  • 2
  • 7
  • 22
19
votes
1 answer

Pretty URLs in PHP frameworks

I know that you can add rules in htaccess, but I see that PHP frameworks don't do that and somehow you still have pretty URLs. How do they do that if the server is not aware of the URL rules? I've been looking Yii's url manager class but I don't…
Alex
  • 66,732
  • 177
  • 439
  • 641
19
votes
3 answers

Regex in mod_rewrite to match URLs NOT ending with this OR that

What is the regex for this? Match if string NOT ( ends in .php OR ends in .html OR contains / ) Thank you! Edit: I need the NOT part because the expression is to be used on an Apache mod rewrite, since I can't change the logic of mod_rewrite to…
johnjohn
  • 4,221
  • 7
  • 36
  • 46
19
votes
2 answers

CakePHP subdomains with htaccess

I've used the following rules in my htaccess file in other applications to redirect users from a folder to a subdomain but load the content from that folder when accessing that subdomain. # REWRITE SUBDOMAIN TO FOLDER RewriteCond %{HTTP_HOST}…
Cameron
  • 27,963
  • 100
  • 281
  • 483
19
votes
4 answers

How to use htaccess to rewrite url to html anchor tag (#)

I have a situation where I want to take the following URL: /1/john and have it redirect using Apache's htaccess file to go to /page.php?id=1&name=john#john so that it goes to an html anchor with the name of john. I've found a lot of reference to…
Lee L
  • 191
  • 1
  • 1
  • 3
19
votes
1 answer

RewriteRule ^ - [L] AKA RewriteRule caret dash L

While I could turn on Rewrite Logging and mess with this rule to figure out what it does (if anything), I bet someone already knows. Here's the rule by itself: RewriteRule ^ - [L] Here it is in context: RewriteCond %{REQUEST_FILENAME} -f…
Dave Scotese
  • 498
  • 5
  • 17
19
votes
3 answers

Request exceeded the limit of 10 internal redirects due to probable configuration error.?

i have a simple rewrite RewriteRule ^.*$ addnew/$0 however i get the Request exceeded the limit of 10 internal redirects due to probable configuration error. I am trying to rewrite www.mysite.com/anycharacter into…
fjfjwo
  • 323
  • 2
  • 3
  • 7
19
votes
3 answers

Apache rewrite rule with parameters?

I have the following URL: http://domain.com/index.php?m=feedback&cSubject=My Subject I want to have a rewrite rule so that the following: http://domain.com/feedback?Subject=My Subject maps to the previous url. Heres my rule at the…
James
  • 80,725
  • 18
  • 167
  • 237
19
votes
2 answers

'hash' url rewrite in .htaccess

I want to rewrite http://example.com/articles.html#first-article with http://example.com/articles/first-article Is it possible to rewrite? I tried using following but didn't work for me: RewriteRule ^articles/(.+)$ /articles\.html\#$1
blue ghhgdtt
  • 907
  • 4
  • 11
  • 16
19
votes
1 answer

How to exclude a specific file from a rewriterule

I have been here: How to exclude a specific file in htaccess and here: exclude files from rewrite rule in .htaccess but neither worked. What might I be doing wrong? .htaccess file: RewriteEngine On RewriteRule…
Christian
  • 735
  • 3
  • 10
  • 29
19
votes
5 answers

mod_rewrite based on ip

I'd like to implement mod_rewrite to put my site into maintenance. Basically all IP addresses except a handful we specify would be forwarded to a static html page. Please can someone help with this rule. Also is there a way to turn this on and off…
Josh
  • 6,256
  • 2
  • 37
  • 56
19
votes
4 answers

How can I use .htaccess to hide .php URL extensions?

I want something to put in my .htaccess file that will hide the .php file extension for my php files, so going to www.example.com/dir/somepage would show them www.example.com/dir/somepage.php. Is there any working solution for this? My site uses…
Markum
  • 3,919
  • 8
  • 26
  • 30
18
votes
6 answers

How to disable Apache gzip compression for some media files in .htaccess file?

I would like to disable gzip compression for some media files which are already compressed on an Apache server via the .htaccess file. Reason: as it's written on e.g. jPlayer's site, gzip encoding should be disabled for media files: "Media files are…
Sk8erPeter
  • 6,899
  • 9
  • 48
  • 67
18
votes
1 answer

Do I need to use ProxyPassReverse if I'm using mod rewrite?

I am using mod rewrite to mask the context root of my application. For example, RewriteRule ^/directory/(.*) balancer://appcluster/directory/$1 [P] The appcluster looks like this: BalancerMember…
Scoota P
  • 2,622
  • 7
  • 29
  • 45