Posts Tagged force trailing slash

Cloak PHP Files as Directories and Force Trailing Slash via mod_rewrite

In an effort to turn a client's website to be more SEO friendly, they wanted to have all PHP files appear as directories on URLs i.e. http://dotmanila.com/blog/php-cloak-and-force-traling-slash.php will end up as http://dotmanila.com/blog/php-cloak-and-force-traling-slash/. We've thought of creating a simple controller combined with mod_rewrite to handle this change on their 30+ websites, however it'll be inefficient to use a two-fold process. So we toyed with mod_rewrite and came up below.

Options +FollowSymLinks
RewriteEngine On

RewriteBase /

# These first set of rules makes sure that visitors
# are viewing the WWW domain i.e. www.dotmanila.com
Rewritecond %{HTTP_HOST} !^www\.dotmanila\.com
RewriteRule (.*) http://www.dotmanila.com/$1/ [R=301,L]

# The next set of rules checks that if the URL
# does not have a trailing slash and
# the requested file/directory when appended
# with the .php extension physically exist on the server
# the we will append a traling slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.dotmanila.com/$1/ [L,R=301]

# The last set of rules makes sure that if
# the requested URL is in proper format the
# corresponding PHP file is mapped.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/$ /$1.php [NS,L]

Cheers!

, , ,

No Comments