8

I'm trying to put together some htaccess code that will turn example.com/filename.php into example.com/filename/ (and force the slash) - I've tried varous approaches, but each hasn't worked quite right, from 500 errors on subfolders to issues with the trailing slash, etc...

Please help!

4
  • 1
    You haven't actually mentioned the approaches you tried - what are your current set of rewrite rules (assuming you're using mod_rewrite)? If you're not using mod_rewrite, what are you using? Commented Jul 1, 2009 at 12:10
  • RewriteBase / RewriteRule ^()$ index.php [NC,L] Rewritecond %{REQUEST_URI} !(^/?.*\..*$) [NC] RewriteRule ^(.*)$ $1.php [NC] Currently works fine to remove the .php extension from files regardless of folder depth. However, adding a / results in a 404 error, and all attempts at integrating a forced slash solution have caused problems. Commented Jul 1, 2009 at 12:16
  • Accept an answer please, this question is being linked too from other questions. Commented Dec 9, 2011 at 2:16
  • @Jono Alderson So which answer is accepted here? Commented Nov 1, 2013 at 22:52

6 Answers 6

13

Try this:

RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]

RewriteRule (.*)/$ $1.php [L]

The first rule redirects requests of /foo/bar.php externally to /foo/bar/. And the second rule rewrites requests of /foo/bar/ internally to /foo/bar.php.

And to force the trailing slash, try this rule:

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
Sign up to request clarification or add additional context in comments.

Comments

6

This solution of Gumbo works great for files, yet it does not work with directories. In other words:

For mysite.com/file1.php, it shows mysite.com/file1/ which is great. Yet, it does not work well for directories. If I try to access the following directory (that contains index.php file inside) mysite.com/dir1, instead of showing the content of the http:/mysite.com/dir1/index.php and the url: mysite.com/dir1/, it returns 404.

My solution around it was:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]


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

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]

In other words, not to do anything if its a directory.

Hope it helps.

Another problem with the original solution is that css and the images are not loaded until I change the path to the css file and to images to absolute path.

Is there any other way to solve it, rather then changing all the paths in all the files in the website to absolute.

Thanks a lot.

1 Comment

This is not an anwer to Jono Alderson’s question. Please open a new question.
2
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

Works like a charm - thanks for the help folks.

Comments

0

You could try working off the question here. Whilst the solution to the question isn't relevant (yet...) the question itself provides a set of rewrite rules which you may be able to use in your own site.

If you require symbols in URLs you could just use ".*" instead of the specific A-Za-z0-9, but if you're looking for a possible trailing slash you may want to use ".*?" instead. This is a standard regular expression feature to avoid the greediness of ".*".

2 Comments

Hmm. That helped a little, and it's lead to me now using: RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^([^/]+)/$ $1.php # Forces a trailing slash to be added RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$ RewriteRule (.*)$ /$1/ [R=301,L] This works perfectly (strips .php. and forces a trailing slash), except when in a subdirectory, when it gives a 404 error :(
!(\.[a-zA-Z0-9]{1,5}|/)$ should probably be !(([a-zA-Z0-9]/?)+)$. If I'm not mistaken that will match all alphanumeric characters followed by an (optional) "/", and match that one or more times.
0

For the path you can add <base href="yourpath" /> to your php pages.

Comments

0

Try this, this one works on my Apache, even when you remove manual the last slash:

Options +FollowSymLinks -MultiViews
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.