Forum Moderators: phranque

Message Too Old, No Replies

Removing extension and adding trailing slash with .htaccess

         

Hubbard

10:33 am on Jun 24, 2006 (gmt 0)

10+ Year Member



Hi guys

This is my situation.

My URLs currently look like this:

[domain.com...]

I have managed to get rid of the PHP extensions with .htaccess and the following instructions:

# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]

Now I need help adding a slash to the end so that my URLs will look like:

[domain.com...]

Please help. I have been Googling like mad!

jdMorgan

2:18 pm on Jun 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you're trying to Google for a specific coded solution, your time would be better spent reading the mod_rewrite and regular expressions documentation; These two combined provide so many ways of doing things, that you may find a million valid code snippets, none of which do exactly what you want. The solution is to understand how it works, at which point the answer becomes trivial.

# remove .php; use THE_REQUEST to prevent infinite loops
# due to DirectoryIndex redirects to .php files
RewriteCond %{THE_REQUEST} ^GET\ /[^.]+\.php\ HTTP
RewriteRule ^([^.]+)\.php$ [b]$1/[/b] [R=301]

I modified the ".*" regex patterns for much more efficient operation as well.

Jim

Hubbard

3:10 pm on Jun 24, 2006 (gmt 0)

10+ Year Member



Thanks so far, jd. It attempts it but isn't quite there yet. This is what's happening:

I go to my site say [domainname.com...]

I click on a link. The actual link to the file is say [domainname.com...]

With your commands in .htaccess the browser attempts to go to [domainname.com...]

However the browser either 403s or it removes the slash and I get what I had before: [domainname.com...]

Or it interprets [domainname.com...] to be a folder, and if there is a folder link/ then it shows me the directory contents or index.

Hubbard

3:42 pm on Jun 24, 2006 (gmt 0)

10+ Year Member



Obviously once we find the solution I will have to rename some files so that link/ doesn't go to a folder called link