4

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?

0

1 Answer 1

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
3
  • 1
    +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".
    – MrWhite
    Commented Aug 31, 2013 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
    Commented Aug 31, 2013 at 18:35
  • @ADTC Please see our Help Center on what comments should and shouldn't be used for. If you have another answer, then please provide that separately, but note that this question is in regards to SEO and having two URLs, which I'm answering is OK to have. We have many other questions here on how to just rewrite file extensions. If you have other questions that the OP isn't specifically asking about (e.g., error pages, handling more directories or slashes, etc...), then please ask that as a new question (after searching the site).
    – dan
    Commented Dec 12, 2016 at 0:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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