Mod Rewrite

Also available in Deutsch

1. Basics

This is a module for the Apache webserver (v1.x and v2.x) and rewrites requested URLs on the fly. It operates both in per-server context (Apache v1.x: httpd.conf, Apache v2.x: apache2.conf) and per-directory context (.htaccess). Detailed informations can be found on the Apache Module Homepage.


So what does it to WackoWiki?!

  1. mod_rewrite enabled:
    • http://www.example.com/Download
  2. mod_rewrite disabled:
    • http://www.example.com/index.php?page=Download
    • http://www.example.com/?page=Download (since 5.5)

That's it ;-)


2. Howto enable mod_rewrite

Of course, you need an Apache webserver WackoWiki is running on, and the module mod_rewrite has to be installed and activated.


mod_rewrite in your Apache configuration file (http.conf oder apache2.conf):
/apache/conf/http.conf


deactivated

#LoadModule rewrite_module modules/mod_rewrite.so

activated:
LoadModule rewrite_module modules/mod_rewrite.so

2.1. AllowOverride All

Make sure that your .htaccess files are not ignored: in your apache configuration file (http.conf or apache2.conf), make sure that for the directory where the wiki is:

AllowOverride None

is not set. Because then .htaccess files are ignored.
AllowOverride All

3. .htaccess

If you have no privileges to edit the apache2.conf (e.g. you rent webspace) or simply you don't want to, you have to use the .htaccess-method (don't forget the dot!) with the following content:

3.1. 5.0 and on

<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteCond    %{REQUEST_FILENAME}  -d
 RewriteRule ^(.*/[^\./]*[^/])$ $1/
 RewriteRule ^admin\.php$ admin.php [L]
 RewriteRule ^(robots\.txt)$ robots.txt [L]
 RewriteRule ^(sitemap\.xml)$ sitemap.xml [L]
 RewriteRule ^(favicon\.ico)$ favicon.ico [L]
 RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]
</IfModule>

3.2. 5.5 and on

# No user serviceable parts inside
# If you want to fix anything by tuning htaccess - you're possibly on the wrong path

<IfModule mod_env.c>
	SetEnv HTTP_MOD_ENV on
</IfModule>

<IfModule mod_rewrite.c>
	<IfModule mod_env.c>
		SetEnv HTTP_MOD_REWRITE on
	</IfModule>
	RewriteEngine on
	RewriteRule ^ index.php [QSA,L]
</IfModule>

<IfModule !mod_rewrite.c>
<FilesMatch \.php$>
	# Apache 2.4
	<IfModule mod_authz_core.c>
		Require all denied
	</IfModule>
	# Apache 2.2
	<IfModule !mod_authz_core.c>
		Order Allow,Deny
		Deny from all
	</IfModule>
</FilesMatch>

<FilesMatch "^(admin|index)\.php$">
	# Apache 2.4
	<IfModule mod_authz_core.c>
		Require all granted
	</IfModule>
	# Apache 2.2
	<IfModule !mod_authz_core.c>
		Order Allow,Deny
		Allow from All
		Deny from None
	</IfModule>
</FilesMatch>
</IfModule>


This file is shipped with the WackoWiki package you downloaded, you find this file inside the Wacko folder. If you can't find it, just create it (Permission: -rwxr-xr-x).

4. httpd.conf / apache2.conf

If you want to edit the httpd.conf / apache2.conf file directly, please make sure to delete or rename the .htaccess in your Wacko folder.
Now add the following lines to your configuration file:

#Wacko URL-rewriting

    Options Indexes Includes FollowSymLinks MultiViews
    AllowOverride All
    RewriteEngine on
    RewriteRule ^(index.php?.*)$ - [S=1]
    RewriteRule ^(.*)$ /wacko/index.php?page=$1 [QSA,PT,L]
    Order allow,deny
    Allow from all

Restart your webserver to enable the modifications!

5. Special cases

special .htaccess files

5.1. RewriteBase

If your webserver's URLs are not directly related to physical file paths, you will need to use RewriteBase in every .htaccess file where you want to use RewriteRule directives.

RewriteBase /

RewriteBase /folder/


e.g. .htaccess

<IfModule mod_rewrite.c>
 RewriteEngine on
 
 RewriteBase /doc/
 
 RewriteCond    %{REQUEST_FILENAME}  -d
 RewriteRule ^(.*/[^\./]*[^/])$ $1/
 RewriteRule ^admin\.php$ admin.php [L]
 RewriteRule ^(robots\.txt)$ robots.txt [L]
 RewriteRule ^(sitemap\.xml)$ sitemap.xml [L]
 RewriteRule ^(favicon\.ico)$ favicon.ico [L]
 RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]

</IfModule>


related

  1. http://httpd.apache.org/docs/c[..]ite.html#rewritebase
  2. bugs:397#bugnotes