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

.htaccess and mod-rewrite

This is my code in .htaccess: RewriteEngine On RewriteRule ^(.*)$ framework/public_html/index.php/$1 [PT,L] I'm getting "Internal Server Error". What's wrong?
Jazi
  • 6,569
  • 13
  • 60
  • 92
3
votes
1 answer

.htaccess Wildcard Redirect Fix

I am using the following code to redirect wildcard subdomains (*.domain.com) to their coresponding folder in /users and redirect direct requests to the /users folder to the subdomain version: Protect Direct Access to Wildcard Domain…
dSquared
  • 9,725
  • 5
  • 38
  • 54
3
votes
2 answers

mod_rewrite, can't rewrite existing file

I'm trying to pass all images in a directory to watermark.php using mod_rewrite in .htaccess. RewriteEngine On RewriteCond %{REQUEST_FILENAME} -f RewriteRule \.jpg$ /watermark.php?path=%{REQUEST_FILENAME} [L] It works fine on local machine, but on…
sevenWonders
  • 175
  • 1
  • 9
3
votes
2 answers

.htaccess Redirect sub-folder

Hi I'm not a programmer by any stretch of the imagination and am trying to do a multi 301 redirect in my htaccess file based on the following: So I have a ton of urls all with similar naming conventions - here is a sample of…
user962876
  • 31
  • 3
3
votes
2 answers

RewriteRule that will force a trailing slash

Currently I have a folder [contact-us] mydomain.com/contact-us/ Im planning to remove this folder and will just add a contact-us.php mydomain.com/contact-us.php How can I create a RewriteRule that will map mydomain.com/contact-us/ ->…
Paul Sanchez
  • 2,391
  • 2
  • 15
  • 9
3
votes
1 answer

How can I rewrite s domain name to the original IDN not the punycode?

I have bought an IDN domain name with non-latin characters. It is good but when I access the domain name, the address bar shows the punycode for the domain not the actual domain which will be hard for any user to remember. Is there is anyway I can…
alhoseany
  • 761
  • 9
  • 28
3
votes
2 answers

Redirect based on the beginning of the path

I'm building a Laravel project. The project itself has a "legacy" version with a whole different structure. It's logged that some users are trying to access a file that is not found in the new environment. For a quick "fix", we want to redirect…
Aldo Adhi
  • 95
  • 3
  • 6
3
votes
3 answers

URL Rewriting/Regex Debug

I need a set of fresh eyes on this. I'm having a tough time spotting the problem. In folder X I have an .htaccess file with the following two lines in it: RewriteEngine on RewriteRule ^([A-Za-z0-9\.-]+)/?$ item-display.php?bibid=$1 [NC,L] My…
Cory Dee
  • 2,858
  • 6
  • 40
  • 55
3
votes
1 answer

Force https via .htaccess with one post http url exception made using fetch

I am able to force https for a domain using this code: RewriteEngine On RewriteCond %{ENV:HTTPS} !on RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] RewriteRule ^index.html$ / [L,R=301] RewriteCond %{THE_REQUEST} /([^.]+)\.html…
vin shaba
  • 167
  • 3
  • 12
3
votes
1 answer

Redirect all URL except one at alias domain

I have two subdomains private, public (but public is only alias of private) and one main domain www. I need to redirect all URLs from public subdomain to www, except existing PDF files and one URL address. I have these rules which work fine, but I…
sylar32
  • 197
  • 3
  • 16
3
votes
2 answers

301 Redirecting from old page to new page on Apache not working

A simple 301 redirect is not working in this instance - For example: Redirect 301 /oldpage http://www.mysite.co.uk/newsubdir/newpage The site is dynamic and the .htaccess is already renaming pages to search engine friendly URL's from URL's…
zigojacko
  • 1,983
  • 9
  • 45
  • 77
3
votes
3 answers

Apache does not rewrite request if file on path exists

I'm doing a rewrite with mod_rewrite on every request that does not match an existing file or directory. This is my configuration: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^.*$ /index.php…
Niko
  • 26,516
  • 9
  • 93
  • 110
3
votes
1 answer

What is wrong with my mod_rewrite Default Image

Need to redirect all non-existing images to the default.gif For some reason the next code does not work Please help. RewriteEngine On RewriteBase /jasper/ RewriteCond %{REQUEST_FILENAME} !-f RewriteRule…
3
votes
1 answer

Combine 2 .htaccess Files

I have 2 .htaccess files that I need to merge together. One is generated by wordpress and the other is the existing .htaccess file for the site. The 2 files are as follows: RewriteEngine on RewriteCond %{HTTP_HOST} !^www\.mywebsite\.com$ RewriteRule…
Jason Small
  • 1,064
  • 1
  • 13
  • 29
3
votes
1 answer

Rewriting URLs and allowing multiple Query Strings

I want to rewrite URLs while still allowing multiple optional Query Strings. For example, I know I can do this: RewriteCond %{QUERY_STRING} ^page=(\d+)$ RewriteRule ^products/?$ products.php Is it possible to allow more query strings to be passed so…
Ricky
  • 31
  • 2
1 2 3
99
100