skip to Main Content

for our client project we need to redirect all url calls to the matching .php file extension. this is done for SEO (google) ranking. avoiding indexing both url (with *.php and without)

we try to do this using the .htaccess file (shared host) but it seemed to only work for redirects that have a different url and not to the one that just adding the “.php” extension.

different url works

abc -> abc2.php

same url doesnt work

abc -> abc.php 

here is our code sample:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.abc.co.uk
RewriteRule (.*) http://www.abc.co.uk/$1 [R=301,L] 

# this doesn't work
Redirect 301 /test http://www.abc.co.uk/test.php

# this redirect works as the url is different
Redirect 301 /test-abc http://www.abc.co.uk/abc.php

we host with 1&1 shared host server.
php version is 5.5

I also tried this code that redirects but the page not loading now:

RewriteRule ^(.+).(.*)$ http://abc.co.uk/$1.php [R,NC]

also noticed url without file extension (.*|html) will not redirect

2

Answers


  1. Try this .htaccess in DocumentRoot:

    Options +FollowSymlinks -MultiViews 
    RewriteEngine on
    
    RewriteCond %{HTTP_HOST} !^www.abc.co.uk$ [NC]
    RewriteRule (.*) http://www.abc.co.uk/$1 [R=302,L] 
    
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.+?)/?$ /$1.php [L,R=302]
    
    Login or Signup to reply.
  2. This appears to be a 302 re-direct, which is not SEO freindly. I tested the above and it doesnt work, also, if it did I wonder what would happen to url’s that did not require the .php extension on the URL.

    Basically we’re trying to get a 301 re-direct working, but it seems to be conflicting with the rules set up that contain the same url. (IE abc > abc.php)

    I wonder if there could be some kind of Apache setting or even and unlikely a PHP setting? the 301’s work on any other linux server that I try.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search