Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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:

wwww.**.com/pageName

I want it to show like so:

wwww.**.com/pageName/

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

share|improve this question

1 Answer 1

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 /

then

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

 
discard

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.