0

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.

1 Answer 1

1

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]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .