0

I'm using a standard snippet to route requests for "myfile" to "myfile.php", using Apache:

# Add .php-extension to base URLS using rewrite (so links can omit it)
<IfModule mod_rewrite.c>
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}\.php -f
  RewriteRule ^(.*)$ $1.php
</IfModule>

and it works. When I type "mydomain.com/myfile" in the browser, I see the page load, with no ".php" in the URL bar. Great.

But now it's also quite clear that I can still continue to still view the page at the old location "mydomain.com/myfile.php". And there are a few problems with that:

1) SEO: I'm diluting link juice, splitting the content between two URLS, one with ".php" and one without. Not crucial, but hey...

2) Any existing public links to the site out in the world will continue to see the unprettified URLs. Yech.

3) Most importantly, all my existing internal navigation links will continue to point to the unprettified URL. Dealbreaker.

My head starts to spin at this point. I think what I want is to rewrite the non-".PHP" version to the ".PHP" on the server, but also redirect the old links with the extension to the extension-less content. Is a there a standard, "best practices" way around this conundrum? Should go through my navigation and strip out all the ".php" file extensions from the links to get rid of the extensions once and for all? Seems like the wrong path, but it's occurred as a possibility :) Even more severely, would it be safe to have PHP files saved without extensions in a LAMP stack? (I've never even tried, too conditioned to avoid it). How do I make the transition to pretty URLS complete?

Thanks in advance for any guidance.

Ben
  • 11,082
  • 8
  • 33
  • 47

1 Answers1

1

Short Term

For all of your non-pretty links have a RewriteRule to redirect to their corresponding pretty link with status=301 (permanent redirect). That way search engines will start caching your new pretty links while gradually discarding all of your old non-pretty links. Here is the .htaccess stuff for that part:

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

## To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ /$1.php [L]

Long Term

Consider converting your internal non-pretty linkes with pretty links (remove \.php).

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • I have a similar confusion as Ben: I'm employing rules like `RewriteRule ^category/subcategory/?$ Category_Index.php [NC]`, but I'm unsure if I'm actually maximizing link juice to the new url's this way. I do know I implemented these about a month ago and many of the old url's are STILL showing up in Google. – user3569292 Dec 15 '14 at 16:05
  • If you've used `R=301` then it will will be fine after some time. However feel free to ask a new question by clearly describing your question. – anubhava Dec 15 '14 at 16:15