1

working with .htaccess i solved about the url conversion from (for example):

(a) http://www.mydomain.ext/dir/myfile.php

to:

(b) http://www.mydomain.ext/dir/myfile

(deleting the .php) to end. But if user from web typing as in (a) them display the url so as typed. How i can solve becouse typing as in (a) webserver display too as in (b) ? I thinked about a redirect from (a) to (b) but not think that it is the best solution, what suggest for solve it? Thanks very much for help.


ADD THIS: Try to explain better the question with a concrete example. Starting from .htaccess i have:

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

and i solved it using PHP so:

$default_url = "http://".$_SERVER["HTTP_HOST"].$_SERVER

["REQUEST_URI"]; // display something as: http://www.mysite.ext/dir/filename.php
    $canonical_url = str_replace(".php", "", "http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]); // display something as: http://www.mysite.ext/dir/filename
    if ( $default_url != $canonical_url ) {
        header( 'Location: ' . $canonical_url, true, 301 );
    }

I ask as update the .htaccess code for integrate about feature descripted in PHP script. I have looked much example but not solve it. Thanks very much.

Marcello Impastato
  • 2,263
  • 5
  • 30
  • 52
  • https://github.com/phanan/htaccess – hjpotter92 Aug 18 '16 at 17:51
  • @hjpotter92 thanks for link, looking about post if type url as in (a) the webpage load correctly and in browser i display as in (a). When i type url as in (b) the webpage load correctly and i display as in (b). I want solve about this problem: if i type as in (a) i want display as in (b). It of course in url field of the browser. I hope to have explained better now with example. – Marcello Impastato Aug 18 '16 at 18:17
  • @anubhava , my problem not is allow to a web page to be loaded without *.php extension from url (i have solved it. I told it in my post). My problem is other, When i type a url putting *.php extension the page is loaded (as same url without *.php) but into field URL of the browser remain the URL having *.PHP extension. I want just to do in mode which typing something as: http://www.pippo.it/page.php in the browser where is URL displayed, i have : http://www.pippo.it/page. Solution is a reload page with a redirect to: http://www.pippo.it/page but how i need to do it with htaccess? Thanks. – Marcello Impastato Aug 18 '16 at 23:20
  • PS: In this sense i don't see this topic as a duplicate, becouse it, yes allow to load page without *.PHP but not remove it from URL displayed on the browser. – Marcello Impastato Aug 18 '16 at 23:22
  • @anubhava i have looked where you suggest me, i have tried but not work. In my post i have added an example concrete, can look better my example so you can understand better about what i want to do? Thanks again. – Marcello Impastato Aug 19 '16 at 07:48
  • @anubhava i looked about htaccess for externally redirect, i have tried to add that line into htaccess (removing php code, of course) and check, but not work. My problem is this. My htaccess is places in root directory. – Marcello Impastato Aug 19 '16 at 08:24

1 Answers1

1

To convert my comments into an answer, have these rules in your root .htaccess:

RewriteEngine on

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Ok thanks very much. So work fine! I don't want abuse of your patience, but can i ask you a last things? When source url point to directory (eg. http://www.mydomain.ext/mydir) i get something as: http://www.mydomain.ext/mydir/ so i ask you as i can clean last character and to have: http://www.mydomain.ext/mydir only. I have searched here about it: http://stackoverflow.com/questions/21417263/htaccess-add-remove-trailing-slash-from-url but not helped me much, can tell me better as i can solve? Thanks again very much. – Marcello Impastato Aug 19 '16 at 10:47
  • If URL is pointing to a directory then you must keep trailing slash in the end for security reasons. That's the reason Apache's `mod_dir` module automatically adds a trailing slash for directory URLs. – anubhava Aug 19 '16 at 10:54
  • To elaborate further, [read this official documentation on `mod_dir`](http://httpd.apache.org/docs/current/mod/mod_dir.html#directoryslash) and do read **Security Warning**. – anubhava Aug 19 '16 at 11:08