views:

41

answers:

1

The below rule I took from here and modified it (very little) so it adds a slash after removing the php file extension.

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)/$ $1.php

So the url Domain.com/file.php will look like Domain.com/file/

And it works fine as long as I have a link or type in directly Domain.com/file/ but I want to keep all my links to my php files. So I want it to redirect someone to Domain.com/file/ if someone goes to Domain.com/file.php

But when I try to redirect (if I'm doing this properly)

RewriteEngine on

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

But It doesn't redirect. :(

I'm not sure if I'm redirecting in correctly or if I'm simply not allowed to do this. Any advice is awesome.

A: 

I'm not entirely sure how it works with htaccess, but you could always just use php. Make this a file and include it at the top of all your pages:

<?php    
  $page = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
  if(substr($page, -4)=='.php'){
    $pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
      $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
      $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"];
    } else {
      $pageURL .= $_SERVER["SERVER_NAME"];
    }
    header( "Location: $pageURL/" + substr($page, 0, -4)  );
  }
?>

EDIT: If you don't care about what the user sees in the address bar, just add an [R] (try taking away the =301,L part)

aharon
Thanks for answering!... But I would really like to use .htaccess because then I will not have to edit every file (that's the only reason I want to redirect or I might as well change all my links)
SuperPaperSam
See edited response. That's untested, though.
aharon
It doesn't work (I tried that before I used R=301,L) and just tried it again.
SuperPaperSam