views:

30

answers:

1

I have a web site that has the file extension of .php. As of right now I have it setup to rewrite that server side using .htaccess to drop the .php from the file name making it example.com/filename, I also have it set to redirect/filename to /filename/. I now want to make it redirect /filename.php to /filename/. How can I do this?

What I have now is:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([^/]+/)*[^./]+)/$ /$1.php

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

This will do the redirect and send the 301 status code, which tells browsers and search engines that it is a permanent redirection.

RewriteRule ^(.*)\.php$ $1/ [R=301,NC]
Sohnee
So I would add this right after: RewriteRule (.*)$ /$1/ [R=301,L] Correct? The first section removes .php, the second redirects without / to /, and what you have redirects .php to /
Brandon
Yes - pop it where you suggest, the redirect takes the bit before the ".php" and pops it in before the "/", so "hello.php" should redirect to "hello/"
Sohnee
When I add it there, the browser returns a redirect error. Maybe there is too many redirects going at once?The L in the last line of my code means Last correct?
Brandon
Ah, it means "Last if this rule matches", this stops .htaccess rules from being re-evaluated after the rule has been matched.
Sohnee
So I need to remove the L from the first one and leave it out or add NC to both of them?
Brandon