I have came up with the following rewrite rule that handles the php files as directories:
www.domain.com/name.php -> www.domain.com/name/
www.domain.com/name -> www.domain.com/name/
www.domain.com/name/ -> www.domain.com/name.php
Above things works, however when a title has a hyphen (-) sign in it then it won’t add the trailer slash on the end and it will end up as a 404 page.
Example that don’t work:
www.domain.com/stack-overflow -> 404 page
www.domain.com/stack-overflow/ -> 404 page
The current rewrite code:
# 404 page
ErrorDocument 404 /404.php
# Add automatic a trailer slash on the end
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !#
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
# Decline direct access to .php files, redirect .php to name/
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^(\w+)\.php$ $1/ [R=301]
# name/ to the right .php file
RewriteRule ^(\w+)/?$ /$1.php
Has someone an idea to handle titles with hyphens (-) in it?
Thanks in advance.
Kind regards,
M
Just replace this:
… with this:
Apache’s mod_rewrite basically follows the Perl regexp syntax.