B K
B K

Reputation: 39

How To Stop Displaying php extension after Redirect from www to non-www

I wanted to hide the php extension so writed

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

I want to force redirect www to non www so i writed the htaccess code

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

It redirecting nicely www to non www version But Not Perfectly As, when i'm typing url www.something.com it is redirecting to something.com But when giving url www.something.com/test it is redirecting to something.com/test.php

and also when i'm typing url www.something.com/example?id=1 it is redirecting to something.com/example.php?id=1

How To do redirect like www.something.com/test to something.com/test and not to show php extension after redirect.

Upvotes: 1

Views: 63

Answers (1)

deceze
deceze

Reputation: 522081

If your redirect rule comes after your .php rewrite rule, then your problem is that the rewritten URL is getting redirected. I.e.:

  1. First rule is hit, request is rewritten from www.example.com/test to www.example.com/test.php.
  2. The second rule takes effect, redirecting to example.com/test.php.

Either move your redirect rule above the other rule, or stop processing of further rules by adding [L] after RewriteRule ^(.*)$ $1.php.

Upvotes: 1

Related Questions