0

There is something I'm missing here : Remove .php extension with .htaccess

My goal:

Redirect everything from www to non-www

Current code in .htaccess

RewriteEngine on
Options -Indexes
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]

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

Current behaviour:

Redirects everything from www to non-www but adds the php file extension ".php" in URIs (the php files do exists)

E.g:

www.example.com/hello

redirects to

example.com/hello.php

What should I do to avoid the php extension in the URI ?

Community
  • 1
  • 1
Koffee
  • 317
  • 1
  • 2
  • 10
  • Change this `RewriteRule ^(.*)$ /$1.php [L]` to this `RewriteRule ^(.*)$ /$1 [L]` – Ismail RBOUH Jul 29 '16 at 12:32
  • Does this always happen? What if you do `example.com/hello`? – apokryfos Jul 29 '16 at 12:33
  • Possible duplicate of [Remove .php extension with .htaccess](http://stackoverflow.com/questions/4026021/remove-php-extension-with-htaccess) – riya Jul 29 '16 at 12:35
  • http://stackoverflow.com/questions/4026021/remove-php-extension-with-htaccess – Hardik Jul 29 '16 at 12:54
  • `example/hello` works fine. Interesting enough, when removing everything but these two lines of code, the stated behaviour is the same `RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*)$ http://%1/$1 [L,R=301]` – Koffee Jul 29 '16 at 14:23
  • 1
    Clear your browser cache if you'd used `301` previously. It caches it. Test using 302 until your rewrites are working then you can set it permanent. – Panama Jack Jul 29 '16 at 14:59

1 Answers1

0

I'm not really sure why, it's working with the following code.

The advice from @Panama-jack is a strong one : using 302 while working on htaccess rules is indeed the way to go before setting everything 301.

RewriteEngine on
Options -Indexes
Options -Multiviews
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://iPutMyWholeUrlHere.com/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [L]
Koffee
  • 317
  • 1
  • 2
  • 10