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

Problems with Facebook comment plugin when use htaccess

The facebook comment plugin in my PHP site doesn't understand the URL in the href attrib and displays Warning: Link us unreachable. I have tested with a file that doesn't use .htaccess which runs good and doesn't throw any errors or warnings, but…
4
votes
3 answers

POST request to SEO URL Forbidden

I have a basic MVC system that is sending POST data to URLs such as admin/product/add/ But this is giving me an error Forbidden You don't have permission to access /admin/product/add/ on this server. Additionally, a 404 Not Found error was…
MattP
  • 2,798
  • 2
  • 31
  • 42
4
votes
2 answers

Remove hashtag from redirect with RewriteRule

I'm currently redirecting a URL by taking a variable and inserting it into a new URL. Like so: RewriteCond %{QUERY_STRING} &pid=([0-9]*)$ RewriteRule ^vault/displayimage\.php$ photos/%1/? [R=301,L] Unfortunately some links have a hashtag in the old…
Andelas
  • 2,022
  • 7
  • 36
  • 45
4
votes
1 answer

Trapping Bad Behavior with rewrite -phpmyadmin anywhere in URL

I'm hoping someone can help as I'm a bit of a noob when it comes to apache mod rewrite, and getting this one wrong can screw things up pretty bad. While going though my security logs I noticed that almost 50% of the attacker bots had the string…
4
votes
3 answers

.htaccess folder to virtual-folder rewriting

Is it possible with Apache's RewriteEngine to do the following, and how? This is the environment: domain.com has a folder that contains a index.html page, htaccess is set to strip files so domain.com/folder/ opens domain.com/folder/index.html Now…
Emanuele
  • 163
  • 1
  • 2
  • 17
4
votes
1 answer

Applying mod_rewrite rules AFTER DirectoryIndex is applied

My hosting company recently upgraded me from Apache 1 to Apache 2, and I've started seeing some quite different behaviour with my mod_rewrite stuff. Here's my .htaccess file: DirectoryIndex blog.html # Various rewrite rules.
andygeers
  • 6,909
  • 9
  • 49
  • 63
4
votes
2 answers

RewriteRule is redirecting to an absolute path, rather than the proper URL

I've created a bunch of RewriteRules for my website and have had no problem with them on my local setup. Here's a snippet from my .htaccess: Options +FollowSymLinks -MultiViews RewriteEngine On RewriteRule ^news/?$ news.php [L] Locally, when I…
Nick S.
  • 353
  • 3
  • 13
4
votes
1 answer

Rewriting URL and making new URL show in location bar

I'm using the below code to rewrite some URL's: RewriteEngine On Options +FollowSymlinks RewriteBase / RewriteRule ^New_Hampshire/[a-zA-Z0-9_+\-\s]*\.html$ NH/ Keep in mind that there is also this rule below that.. RewriteRule ^([a-zA-Z]{2})/…
Brett
  • 19,449
  • 54
  • 157
  • 290
4
votes
1 answer

.htaccess optimization

Can anyone help with .htaccess optimization? Actually I have next code: RewriteEngine on RewriteBase / DirectoryIndex frontend/www/index.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} cpanel/(.*)$ # Why not "-f" - because it's…
Arthur Halma
  • 3,943
  • 3
  • 23
  • 44
4
votes
1 answer

htaccess missing www

Here is my .htaccess AddDefaultCharset UTF-8 RewriteEngine On RewriteBase /xcrud RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1…
Drew
  • 24,851
  • 10
  • 43
  • 78
4
votes
2 answers

.htaccess RewriteRule returns a blank page

I'm trying to solve this for more than two hours now. I have a personal site which uses .htaccess to manage urls. It looks like this: RewriteEngine on RewriteBase / ... RewriteRule ^sklad/?$ index.php?action=sklad RewriteRule ^sklad/user/([0-9]+)?$…
Afftamat
  • 43
  • 1
  • 4
4
votes
1 answer

Passing $_GET variables to virtual directories via .htaccess

I have the following .htaccess Rewrite rule below which works for converting virtual directories to parameters, for example: www.example.com/usa/ny/nyc gets interpreted by PHP as www.example.com/index.php?path=usa/ny/nyc. What I can't seem to…
Zack
  • 1,615
  • 18
  • 26
4
votes
2 answers

.htaccess rewrite imgname.jpg to image.php?img=imgname

I am attempting to rewrite URLs like the following: domain.com/news/12/imgname.jpg to domain.com/image.php?img=12/imgname I am using the following in my .htaccess file, but it does not seem to be working: RewriteCond %{REQUEST_FILENAME}…
4
votes
2 answers

What is the best way to implement a friendly URL that has multiple variables using mod_rewrite?

I am building a web application that is client side js heavy with data being pushed in chunks from the server. I am trying to implement a solution for a friendly URL solution that takes a URL…
thiswayup
  • 2,069
  • 8
  • 32
  • 52
4
votes
1 answer

How do i rewrite this url using .htaccess

I would like it to show the url /index.php?page=somePage&id=someID&siteAddress=someAddress as /someAddress/?page=somePage&id=someID. How can I use the rewrite rule for this?