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
39
votes
10 answers

.htaccess Rewrite to Force Trailing Slash at the end

I have the following code in my htaccess file: # Force Trailing Slash RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301] That seems to work fine when I go to www.mydomain.com/test it redirects it to /test/. The…
Drew
  • 6,736
  • 17
  • 64
  • 96
39
votes
8 answers

.htaccess: RewriteEngine not allowed here

I uploaded the .htaccess to the server and received an Error 500 (Internal Server Error). And in the error log I had the following error: .../.htaccess: RewriteEngine not allowed here But mod_rewrite.so is enabled. So, do I need to change
user317005
39
votes
4 answers

mod_rewrite: remove query string from URL?

I'm trying to make the following redirection (301) using .htaccess *?page=1 redirects to * (where * is a wildcard). Basically, I just want to prevent anyone accessing a page with ?page=1 at the end of the URL, and instead direct them to the same…
Freddy
  • 393
  • 1
  • 3
  • 4
39
votes
2 answers

RewriteRule Error: Bad flag delimiters

Using this RewriteRule in my .htaccess file I'm getting RewriteRule: Bad flag delimiters which is returning a 500 error in the browser. Can anyone point me in the right direction please. Thanks. RewriteEngine On RewriteCond %{HTTP_HOST}…
Gareth Daine
  • 4,016
  • 5
  • 40
  • 67
39
votes
1 answer

Understanding difference between redirect and rewrite .htaccess

I'd like to understand the difference between redirecting and rewriting a URL using .htaccess. So here's an example: Say I have a link like www.abc.com/ index.php?page=product_types&cat=88 (call this the "original" url) But when the user types in…
Ray
  • 3,409
  • 8
  • 33
  • 40
39
votes
5 answers

Redirect a range of IPs using RewriteCond

Currently I am redirecting all users except for the IP 12.345.678.90 using: RewriteEngine On RewriteCond %{REQUEST_URI} !/maintenance$ RewriteCond %{REMOTE_HOST} !^12\.345\.678\.90 RewriteRule $ /maintenance [R=302,L] What syntax would I use to…
xylar
  • 7,433
  • 17
  • 55
  • 100
38
votes
3 answers

AH10411 error: Managing spaces and %20 in apache mod_rewrite

I have updated Apache today (to 2.4.56-1) and a load of .htaccess rewrites that used to work are now getting AH10411 errors, relating to spaces in the query. I'm struggling for a 'proper' solution. The user clicks on a link such as
Phil Evans
  • 790
  • 9
  • 18
37
votes
5 answers

mod_rewrite with spaces in the urls

I need to set up some RewriteRules to redirect a URL which has a space in it. I've tried this: RewriteRule ^article/with%20spaces.html$ /article/without_spaces.html [R=301,L] ... but it doesn't work. Putting in a space instead of %20 causes a 500…
nickf
  • 537,072
  • 198
  • 649
  • 721
37
votes
4 answers

Redirect to Apache built-in 404 page with mod_rewrite?

Is there a way to actively serve Apache's default, built-in 404 page for a number of URLs using mod_rewrite? Not a custom error document, but a rule like RewriteCond %{REQUEST_URI} ^/dirname/pagename RewriteRule -- serve 404 page ----- I know how…
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
37
votes
1 answer

How can I mod_rewrite and keep query strings?

I want to mod_rewrite a URL to another page, but then I also want any query strings added to be preserved. RewriteEngine On #enforce trailing slashes RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !# RewriteCond %{REQUEST_URI}…
Daniel Oakey
  • 405
  • 1
  • 4
  • 7
36
votes
3 answers

mod_rewrite RewriteCond - is NC flag necessary for just domain part? And some more

I have seen many times in htaccess these type of rules : RewriteCond %{HTTP_REFERER} !^http://www.domain.it$ [NC] or RewriteCond %{HTTP_HOST} !^www\.domain\.it$ [NC] Why is the NC flag necessary, when checking only the domain part? I noticed…
Marco Demaio
  • 33,578
  • 33
  • 128
  • 159
36
votes
5 answers

How to use .htaccess in WAMP Server?

I searched in web for 2 days and I try to use htaccess in my local wamp but I can't! I know there is something wrong but I don't know where... First: I activated "rewrite_module" in the apache menu, then I checked the phpinfo page and I saw that…
Mona
  • 788
  • 3
  • 13
  • 18
36
votes
3 answers

Best Practice: 301 Redirect HTTP to HTTPS (Standard Domain)

I have been searching for the perfect 301 redirect. But I am finding so many solutions and do not know what’s best. Here is what I want to do http://domain.tld/ → https://domain.tld/ http://www.domain.tld/ →…
dash
  • 1,249
  • 3
  • 17
  • 25
35
votes
2 answers

RewriteRule Last [L] flag not working?

php_flag display_errors 1 php_value auto_prepend_file init.php RewriteEngine on RewriteRule ^$ /id/authenticate [R] RewriteRule ^login_openid$ /id/login_openid.php [QSA,L] RewriteRule ^authenticate$ /id/authenticate.php [QSA,L] RewriteRule…
The Surrican
  • 29,118
  • 24
  • 122
  • 168
35
votes
1 answer

Using mod_rewrite to convert paths with hash characters into query strings

I have a PHP project where I need to send a hash character (#) within the path of a URL. (http://www.example.com/parameter#23/parameter#67/index.php) I thought that urlencode would allow that, converting the hash to %23 But now I see that even the…
Mark
  • 1,129
  • 2
  • 12
  • 25