Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

just wondering how I add a trailing slash at the end of my URL's using Mod_Rewrite?

This is my .htaccess file currently:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ index.php?pageName=$1

My URL show like so:


I want it to show like so:


The URL is holding a GET request internally, but I want it to look like a genuine directory.

share|improve this question
up vote 1 down vote accepted

First we need to have '*mod_rewrite*' module enabled.

# a2enmod rewrite

Then make sure your configuration file has

RewriteEngine on

before following rules are applied.

Here is rule that will check if REQUESTED

  • file does not exists
  • request is not for index.php
  • REQUESTED_URI does not end with /


make external redirect (301) to your domain.tld/ with whatever was requested and / appended. Make this last rule of given set.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.tld/$1/ [L,R=301]

Next rule

  • if new request, file does not exist
  • if directory does not exist
  • if request is not for index.php

Then pass through all requested as 'pageName' param to index.php / make this last rule and do query string append.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index.php
RewriteRule ^(.*)$ index.php?pageName=$1 [L,QSA]
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.