mtmacdonald
mtmacdonald

Reputation: 15110

Apache2 mod_rewrite - how to add trailing slash to only one subdirectory

I'm using Apache 2.2 with two servers:

I have an existing rewrite rule for clean urls (to remove 'index.php' from the url).

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

I have one special route that only works if a trailing slash is present:

How do I add a rewrite rule to my existing .htaccess to always add a trailing slash, but only for the documentation route?

Upvotes: 1

Views: 5126

Answers (2)

Don't forget to add these directives first once per .htaccess file:

Options +FollowSymlinks
RewriteEngine on

And then you can try this:

RewriteRule ^/?(documentation)$ /$1/ [R,NC,L]

Or this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} (.+)/([^/]+)$
RewriteRule ^/?(documentation)/(.+)$ /$1/$2/ [R,NC,L]

I'm confused about your question..

Upvotes: 0

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51721

You just need to add another RewriteRule that matches only against /documentation i.e. without a trailing slash. The first rule adds the trailing slash and then your existing rule adds the index.php.

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^(documentation)$ $1/ [NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

If you would like the browser's address bar to also reflect the trailing slash change the rule to

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

RewriteRule ^(documentation)$ $1/ [R=301,NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Upvotes: 3

Related Questions