Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I want to make my URLs clean, for example:

http://sample.com/about-me instead of: http://sample.com/about-me.php

This is my code:

    RewriteEngine On 
    RewriteBase / 
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^ %{REQUEST_URI}.php

But I can still access http://sample.com/about-me.php.

How can I have http://sample.com/about-me, so http://sample.com/about-me.php will be permanently redirected to that?

I think this technique is good for SEO because canonicalization needs just one URL. If I use two URLs that both can be accessed, I think that's not good for SEO and can make search engines confused, right?

share|improve this question
    
Was my answer helpful? If so, comments and upvotes/accepts shows others that you provide feedback - thanks. –  dan Sep 5 '13 at 15:56

1 Answer 1

The reasons for using clean URLs include:

  • They make it easier for users to identify in search engine results and links on websites as to where they would be sent to if clicked on.

  • They make links in search engines and websites appear more relevant to the content that they're interested in, versus a filename or query parameters, and thus users will be more inclined to click on them.

  • They can make it easier for search engines to understand complicated URLs (e.g., if it contains lots of parameters).

It is perfectly OK to have more than one URL point to the same resource, providing that you add a canonical URL to the page to let search engines know the preferred URL to index. Alternatively, you can request that one of the URLs be excluded from indexing in your robots.txt file.

Most users could probably understand a simple URL like the following, and certainly search engines can, so the benefits of converting it into a clean URL are likely not much:

http://sample.com/about-me.php

However, if you still want to remove the PHP extension in the URL, you can try using this in your .htaccess file:

RewriteEngine on
RewriteBase / 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php
share|improve this answer
    
+1 Adding a rel="canonical" would be the preferred solution to resolve the canonical URL issue in this case IMO. You can add an external redirect to .htaccess to redirect ".php" URLs to the bare URL but it's a bit messy and largely unnecessary IMO. There are also other potential issues like trailing slashes etc. and these would all be resolved with a rel="canonical". –  w3d Aug 31 '13 at 9:56
    
Thanks - thought so too, so presented clean URL and canonical URL info first. Agreed that removing PHP extension can present more problems than it's worth if not needed, like infinite loops... –  dan Aug 31 '13 at 18:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.