Hi all,

I'm experimenting with hiding the .php extension for my webpages using the .htaccess file. Seems to work, but I have a few questions...

Let's assume I have a page "www.mysite.com/test.php", and the rewrite changes it to "www.mysite.com/test"

1) I'm assuming my internal links should still reference "test.php"

2) I'm assuming external links should reference "www.mysite.com/test" (without the extension)?

3) What is the impact on SEO?

4) Are there any "cons" to using this technique I should be aware of?

5) Looking at the code below, will this also work for files in deeper subfolders? (i.e. www.mysite.com/folder/page.php)

Any information appreciated! (the code I'm using is below - feel free to offer any suggestions)

Thanks all!

CODE:

RewriteEngine On

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.mysite.com/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.mysite.com/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

What do you mean by "internal links" vs "external links"? All links on your site and off your site should use the rewritten links. There should be no way for a website visitor to know what the real path to the file is, no hyperlinks anywhere pointing to it, etc.

Many years ago, URLs were rewritten because search engine bots had a hard time navigating dynamic sites that had multiple query strings. They would end up in loops recrawling the same page a billion ways with an infinite number of query string parameters. URLs were rewritten to "fake" the behavior of static (non-dynamic) pages. Nowadays, bots have improved and don't really have such a hard time anymore. URLs are rewritten for usability's sake. For example, the path to this current page is:

/web-development/php/threads/462191/url-rewrite-with-.htaccess-what-about-internal-links

From a usability perspective, that URI tells you a lot about the page, and helps define some breadcrumb navigation.

You can of coure rewrite URLs in directories, multiple directories, etc. You can use regular expressions to manipulate anything to look like anything else, pretty much.

Dani - thanks - yes, by internal I meant links within the site pointing to other pages within the site.

So, are there any "cons" to using this approach? Doesn't sound like it.

There could only be a single con
:where a folder name and extensionless filename are the same:
likeliehood, zero, non existent, would only happen to someone not checking design.

Thanks almostbob - I think I'll continue using it - seems like it's a good thing.

So, are there any "cons" to using this approach? Doesn't sound like it.

I think the benefits greatly outway any cons you may come up with. Of course, the URL rewriting introduces the risk of making mistakes with your rules, but that can be mitigated by following a testing process prior to publishing new rules.

Aside from search engine optimization, the clean URLs make your site that much more user friendly.

Another benefit, if you ever have to rearrange your structure, you can write rules to redirect users (and spiders) to the new page while sending back the appropriate HTTP responses.

Here is an example of a set of rules where I used both redirects and rewrites:

Old URL: http://www.anitkb.com/2010/10/how-to-pass-parameters-to-vbscript.html

New URL: http://www.itgeared.com/articles/1075-how-to-pass-parameters-to-vbscript/

Notice that the domain changed as well as the path in the URL. Both point to the same new location now.

Ah, yes, if this isn't a new site, be sure to use 301 redirects to redirect users at the old URI to the new URI.