1

I'm trying to remove the .php extension on my URLs to make it Search Engine Friendly using the .htaccess file Redirect 301, to keep the "rank juice" and I as much I've tried almost every example around - It just doesn't seem to work.

Here are some of the methods I've unsuccessfully tried already:

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

This is the most common solution given but nothing happens and there is no changes.

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

Nothing happens and there is no changes.

RewriteEngine On
# RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
# RewriteRule (.*)\.php$ /$1 [L,R=301]
# RewriteRule (.*)/$ $1.php [L]

I get a Not Found : The requested URL /AMENPTHOME/hostnd/3/9/9/399fc7b78/www/htdocs/web/dive-sites.php was not found on this server.

Amen is my host and the file dive-sites.php in this specific real example is on the root. My goal is to have: www.domain.com/dive-sites and not www.domain.com/dive-sites.php using a Redirect 301 because this url is already ranked for a while. Can someone please help ?

Thank you very much, all help appreciated.

3 Answers3

2

If I didn't missed anything:

RewriteEngine On

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/(.+)\.php[^\s]* [NC]
RewriteRule ^ /%1 [R=301,NE,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [QSA,NC,L]

Requests to /dive-sites.php will issue a 301 redirect to /dive-sites, appending query-strings if any.

Requests to /dive-sites will get a 200 response with /dive-sites.php as Content-Location, appending query-strings if any.

Paulo Freitas
  • 13,194
  • 14
  • 74
  • 96
1

try this:

RewriteEngine on

RewriteRule ^/(.+)(($|#)*(.*)) /$1.php$2

If removing only the .php this will work.
$1 = for the name of the page, e.g foo
$2 = take note of the hashtag or the get request

eg.
<domain>/example ----> <domain>/example.php
<domain>/example?get=this ----> <domain>/example.php?get=this
<domain>/example#hash ----> <domain>/example.php#hash

Community
  • 1
  • 1
Yjae Dalina
  • 106
  • 5
0

Try this code for .php extension removal:

RewriteEngine On

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

# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • I guess the external redirect is working nicelly since it does remove the extension but as for the internally forward I'm still getting: 404 Not Found The requested URL /services was not found on this server. (in this case for the file services.php) – pedroteixeira07 Nov 11 '13 at 12:35
  • What was the original URL? Is this placed in DOCUMENT_ROOT or some other subdir? – anubhava Nov 11 '13 at 12:39
  • Although trough Filezilla they are in the folder /public/www - I'm assuming this is the root since the original files are in the folder www in the same location as index.php. For example: www.domain.com/service.php – pedroteixeira07 Nov 11 '13 at 12:55
  • Exactly. More precisely: The requested URL /service was not found on this server. – pedroteixeira07 Nov 11 '13 at 14:16