Removing file extensions using htaccess

Lately, there seems to be a trend where designers want to cloak their file extensions from their visitors. For example, they want the URL ‘domain.com/index.html‘ to display as ‘domain.com/index‘ for whatever reason. Some like the way it looks, others believe it helps with SEO, and others believe it is better for security. I am unsure the last two hold any bit of truth, however. This is a feature you can implement via your htaccess file. This article will guide you through doing so via your cPanel.

How to remove file extensions from display via htaccess

  1. Log into your cPanel.
  2. Click on File Manager in cPanel

    Find the Files category and click on the File Manager icon.

  3. Show hidden files in settings

    In the top right hand corner, click on Settings. For the primary domain, click on the Web Root radio button. For addon domains, click on the dropdown and find your desired addon domain name. Be sure the checkbox next to Show Hidden Files is checked. Click the Save button to return to the main File Manager screen.

  4. Click on edit

    You should now be in the root folder of the domain you chose. Look for the .htaccess file and right click on it. This brings up a menu. Find and click on the Edit option. If you get a popup box, simply find and click the Edit button in the lower right corner to continue to the editor.

  5. You are now in the text editor. As most sites are coded with either htm or php, we have include the code for both below.
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*)$ $1.php

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^(.*)$ $1.html
  6. Be sure to hit the Save Changes button in the upper right corner to save your new htaccess configuration. Your site should now display the address without the file extension.

Thoughts on “Removing file extensions using htaccess

  • Hello,

    I have tried many diffrent wyf of reminving the html extension and adding a trailin slah at the end on my website’s urls.

    The problem I run into is this:

    I either manage to have the trailing slash adde,d but then for some reaons wyen my site is on a mobile device (with a small menu button on the upper right corner) that button (#menu) does not work anymore.

    OR: the trailing slash is NOT added, in which case the menu button on a mobile device does work.

    I can’t seem to get the trailing slah at the end of the url AND the menu button on my website when on a mobile device to work.

    Any help would great be appreciated!

    thanks!

    Lars

     

     

    • Hi Lars,

      I’m not sure of a way to make these both work. Typically the trailing slash is used to indicate the directory, not necessarily a file. So I would recommend not using trailing slashes since then your menu will be functional.

  • I copied and pasted your code for the html choice into my .htcaccess file. However, the extensions on the website are .htm, so I edited your code from .html to .htm. I’ve cleared my cache. My extensions are still showing. Do I have to take them out of my links in the html of my website also?

    • We are happy to help but will need some additional information. Can you provide a link to the site you are testing?

      Thank you,
      John-Paul

    • This code generally works for custom coded websites. However, you would need to adjust the code to integrate it with your existing code. If you are using a CMS like WordPress or Joomla!, then this code may interfere with the functionality. I recommend reviewing the error(s) you are receiving since adding that code to your website.

  • How the hide .php extension permanently, i used the above code.

    But still, it displays with and without .php. 

    Can anyone provide me the solution for this?

    • We are happy to help but will need some additional information. Can you provide a link to the site for us to test further?

      Thank you,
      John-Paul

  • Hello, i hope this thread is still going. 

     

    I have a website built with one.com website builder. However when i clock on one of my menu tabs to go to that page im getting .html at the end of the url. And if i was to type in the address bar the url i want I.E. (www.example.co.uk/contact) it says error not found. However if i type (contact.html) the page loads.

    My files are named:

    Index.html

    Contact.html

    Our Story.html

    Etc…

     

    in my htaccess file i have:

    <FilesMatch “(?<!^sitemap\.xml)$”>

    ExpiresActive On

    ExpiresDefault “access plus 1 year”

    FileETag none

    Header append Cache-Control “public, immutable”

    </FilesMatch>

    I have tried adding all this code suggested in the htaccess file at the bottom but still dont work.

     

    Also the code in my index.html/ contact.html/etc… all have the links to the oages with .html in them. do i need to remove this bit in every .html file for my pages?

    Also…. once i update my website in website builder it will default back to .html again! 

     

    One.com are useless and have no answer for me.

    • Sometimes the order in which directives are passed in the .htaccess file can make a difference. Try placing the code in this article above the code you currently have. If this has an undesired effect, you can set it back the way it was.

  •  I have use below code but facing “404 Not Found error”

    Please help us to resove this issue.

    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /

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

    • Hello,

      It appears that you’re using the code to make a redirect based on rules for a rewrite. This article provides information on how to remove file extensions so that the viewer does not see them in the URL. We typically do not troubleshoot coding, so you may want to refer to our article on 301 redirects if you want further help on that topic. Otherwise, you may want to consult with a programmer or a community forum associated with providing assistance for that particular topic.

      If you have any further questions or comments, please let us know.

      Regards,
      Arnel C.

  • hey i have this problem. i have no .htacess file in my root folder. but i created one. and insert above code to it. but i have no idea how to change it.. my htaccess file has only that codings. please  help me to resolve this…

    RewriteEngine on
    RewriteCond %about-us.php !-d
    RewriteCond %about-us.php\.php -f
    RewriteRule ^(.*)$ $1.php

    • Hello Gunasekar,

      Make sure that you do not have any caching setup as it will repeat the URL until you clear it.

      If you have any further questions, please let us know.

      Kindest regards,
      Arnel C.

  • hello there..

    My website url is..http://www.skytecsolution.com/…and it have around 40-50 pages and all pages have .html extension..but if i open the any of my webpage without writing .html extension ..it will open the same page for me..and when i created it’s sitemap it will showing me that you have 2 pages of same name..First one is .html and second one is without .HTML..

    like this:-
    http://www.skytecsolution.com/about.html
    http://www.skytecsolution.com/about
    So i want only one page without HTML extension..so that search engines might index these pages as duplicate content..so what;s is the solution..
    Help me…

  • Hello, i just try to hide .file extension from my side and for that i use .htaccess file with your code but by doing this also .html and .php extension is not gone from url please suuggest me where i go wrong.

    • This rule does not erase or hide the file extension if it is still requested in the URL. Rather, it will allow the file to display without the extension. For example, instead of looking up file.html, you can use just file in the URL. However, file.html will still display the page.

  • Hi. I just searched through the comments for answers but was unable to find a solution to my problem.

    Let’s say that I have the subdomain https://shop.mydomain.com Some of my items have direct links like this:

    https://shop.mydomain.com/?item_id=1

    https://shop.mydomain.com/?item_id=2

    https://shop.mydomain.com/?item_id=7

    These items numbers and links are auto-generated in the admin when I upload a new item.

    Is there a .htaccess script that will hide the “/?item_id=#” or will I have to create a code for each item?

    In other words… If I give someone a direct link to https://shop.mydomain.com/?item_id=7, &nbsp;can we have the .htaccess code to auto-hide the  “/?item_id=7” ?

     

    Thanks in advance for your help!!!

    • JP, I would recommend using a rule like the following in your .htaccess file:

      RewriteRule ^items/([0-9]+)/?$ ?item_id=$1 [NC,L] # Rewrite item requests

      This rule will allow you to use links formatted like https://shop.mydomain.com/items/7 which should also be more ‘SEO friendly’.

  • SCOTT, THANK YOU SO VERY MUCH. I’ve been trying for YEARS to get this to work right. I have failed on no less than 14 occasions after untold hours of research, phone calls, etc… THANK YOU. This has been the biggest thorn in my side forever. Your response is the only one I’ve ever had success with. THANK YOU A MILLION TIMES THANK YOU.

    • Hello Mageshwarah,

      The removal of the file extensions during a query string are a little different. Please review this tutorial for information on how to write the rewrite for a query string.

      If you have any further questions or comments, please let us know.

      Regards,
      Arnel C.

  • Hai,

    I have successfully accessing the webpage with/without .php extension using below code.
    Options +MultiViews
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ $1.php [NC,L]

     

    Now I need to remove .php in address bar If I run url with .php extension.If possible please post me the code.

    Thanks ,

    Santosh Kola

     

    • It sounds like you were able to remove the .php extension. I’m not sure what you mean in the latter part of your comment.

  • Sir i have many files that i already redirected by appending .php extension. Is there any way that i could just write .php file and it takes care of it??

    Please provide the solution

    • Hello,

      Sorry for the problem with your PHP extension files. Unfortunately, we cannot provide a coding solution for you. You may find a solution by looking at this forum post.

      If you have any further questions or comments, please let us know.

      Regards,
      Arnel C.

  • Hello @scott This code works fine, but I have one subdirectory, where need to show file extension. How can I show it only one specific folder?
    Thank you

    • When you say you have trouble accessing the admin panel do you mean cPanel? Or, are you having trouble accessing a CMS admin panel? If you are having trouble getting into cPanel then you can use access the direct port.

    • If the file is in a directory by itself you can try setting up a blank .htaccess file in the directory where that file exists, but if there are other files in that directory they may be affected.

  • Hi,

    I am using custom code(PHP).
    is there any way to do it ?

    I tried following code

    RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /play_user_video\?title=([^&\ ]+)&?([^\ ]*)
    RewriteRule ^ /play_user_video/%2/?%3 [L,R=301]

    But it returned “internal error”. I don’t have any idea how to send internal request …. please help me out

    • Hello,

      There is no easy way to do that as you are trying to take out certain sections of a URL and rebuild it. What software did you use to build your website or is it custom coded?

      Best Regards,
      TJ Edens

    • Hello Tapash,

      I went to all three of your websites and was not able to replicate any error while clicking on links and your .htaccess looks like a normal basic WordPress one. Can you explain on how to replicate the errors?

      Best Regards,
      TJ Edens

  • This code works great!

    Options +FollowSymLinks -MultiViews

    # Turn mod_rewrite on

    RewriteEngine On

    RewriteBase /

     

    ## hide .php extension

    # To externally redirect /dir/foo.php to /dir/foo

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]

    RewriteRule ^ %1 [R,L,NC]

     

    ## To internally redirect /dir/foo to /dir/foo.php

    RewriteCond %{REQUEST_FILENAME}.php -f

    RewriteRule ^ %{REQUEST_URI}.php [L]

     

    but why does my $_SESSION variables stopped working?

    • Hello Omar,

      Thank you for contacting us. We are happy to help you troubleshoot website issues. But, this type of problem will require additional access, and more detailed knowledge of the code.

      Can you provide a link to an example, or more information on what you are trying to accomplish?

      Thank you,
      John-Paul

    • Hello Vishnu,

      Unfortunately what you are asking is beyond the scope of this article. The good thing about htaccess redirects is you can keep playing with them until you narrow down the piece you need. We are not able to develop a custom piece of code, however.

      Kindest Regards,
      Scott M

  • Hi, I have a question. I use a hyperlink for clients to download certificates. For example http://www.mysite.co.uk/certificates/1235.pdf

     

    What I am trying to find out is if I can use htaccess for a different file. Lets say for example, we didnt upload a pdf file but used a.mp4 movie.

    What I am looking for in htaccess is if the file is called 1234 and isnt a pdf but is 1234.mp4 it will automaticaly look for either file name on the same link.

    Is this achievable in htaccess?

    Kind regards

     

    Chris

    • Hello Chris,

      We have not tested such a scenario. I’m certain it can be done via code such as php, but have not seen this in htaccess before.

      Kindest Regards,
      Scott M

    • Hello Shayir,

      You can remove the .html extension by putting the following in your .htaccess file:

      RewriteRule ^([^\.]+)$ $1.html [NC,L]

      Best Regards,
      TJ Edens

  • Hi

    am trying to edit the .htaccess file but it seems not to work even after using the code you provided.

    it’s a simple html site and this is the code in my .htaccess file

    # Do not remove this line, otherwise mod_rewrite rules will stop working
    RewriteBase /
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule ^(.*)$ $1.html

    • Hello Arnold,

      The code works on our test servers. Keep in mind it does not remove the .html if you type it in, but it allows you to type in the file without the extension and can use it to create links as such.

      For instance, if you have example.com/page.html you can use either example.com/page or example.com/page.html to see the page. Whichever you type in will still display in the browser.

      Kindest Regards,
      Scott M

    • Hello Bibi,

      Can you please provide the code you used to try and remove the files extensions?

      Best Regards,
      TJ Edens

    • Hello Lance,

      The instructions in the article explain how to use it in the .htaccess file. These instructions are used to remove the extensions from appearing in the URL. If you’re asking for something different, then please let us know exactly what you’re trying to do.

      Regards,
      Arnel C.

    • Hello joy,

      Thank you for contacting us. It depends how your site/hyperlink was created. We are happy to help, but will need some additional information.

      What CMS are you using? For example, WordPress, Joomla, Concrete5, etc.

      Can you provide a link to the site with the hyperlink?

      Thank you,
      John-Paul

  • HI,

     i want to htaccess for like example.com/test.php?para=birds to change as

    example.com/test/birds

    could you tell anyone how to write .htacces for this.

     

    Thank in advance….

     

    • Hello Venu,

      You will need to work with query strings within the .htaccess rule. Checkout this tutorial. Basically, the rule will look like this:

      RewriteCond %{QUERY_STRING} ^para=birds$
      RewriteRule ^test\.php$ http://example.com/test/birds.php [R=302,L]

      I hope this helps to answer your question, please let us know if you require any further assistance.

      Regards,
      Arnel C.

  • Hello..

    I want enable in my custom post type in wp-admin.. I added screen Option but their menu is not dispalying menus like( ).. this type 

    And one other question how to create capability in wordpress means for perticular plugin or post type with perticular name or keyword that is connect to perticular plugin or post type…

    If you know than please share.. 

     

    Many Thanks…

  • i am writing a program to create nested directory and create file at last directory. here is a program, which is creating directories.

    <?php
    
    function make_path($path){
        $dir = pathinfo($path,PATHINFO_DIRNAME);if(is_dir($dir)){returntrue;}else{if(make_path($dir)){if(mkdir($dir)){
                            chmod($dir,0777);returntrue;}}}thrownewException("This Directory is available");}try{
    $url ="http://www.w3schools.com/php/php_ref_string.asp";
    $remove_http = str_replace('http://','', $url);
    make_path($remove_http);
    $tv = explode('/',$remove_http);for($x=0;$x<count($tv)-1;$x++)
    echo "<br/>".$tv[$x];
    echo "<br>".end($tv);}catch(Exception $e){
    echo $e->getMessage();}?>

    ................................................................... this program is creating first folder named 'www.w3schools.com', second folder named 'php' in first folder. but php file is not saving in the second folder. please help.......
    • Hello Sudhakar,

      Thank you for your question in regards to your program. Unfortunately, we do not provide support of that nature with the Support Center. You will need to work with a programmer/developer for help with that issue.

      Kindest regards,
      Arnel C.

  • TJ, I finally found your reply, thank you. The code below if you remove the word folder is a complete site rewrite and it works.

    So, let me give it a shot, and I assume you mean by folder, the .html folder, /public_html, thus:

    RewriteEngine On
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://www.erpsaa.com.com/fpublic_html/$1 [R,L]

    Thanks again, I was at the point of trying to do it page by page in ths way:

    # Permanent URL redirect 

    Redirect 301 http://www.erpsaa.com/contact-us.html https://www.erpsaa.com/contact-us.html

    which worked perfectly, until I added a second page.

    Thanks again,

    Dean

     

  • Hi, I’ve tried everything I can think of and keep ending up with the answer of “folder” which makes no sense. My problem is I have a sitewide EV SSL. I want to force the  SSL to apply to pages with specific extension, namely, .html.  The site is a mix of .php and .html and this is where it gets weird.

    With NO code in the .htacces file, if you manually force the SSL by adding the s to http. for example: https://www.erpsaa.com/ then every single .html page remains secure including the form where clients send us confidential information. PERFECT.

    When you go to any .php menu in the pull down, for example: http://erpsaa.com/solutions-vmware-best-in-class-virtualization.php

    It kicks off the s, which is what I want because I share content with VMware and the .PHP pages will become distorted if the SSL is applied. However, they seemed to knock the site right out of secure. THE PROBLEM: Once out of secure, leave the any of the .php pages and the .HTML fails to engare the SSL, for the simple reason is that I did it manually in the first place. So why, when I redirect the entire site am I still in a mess, since the code below obviously directs the entire site from http to https permanently:

    RewriteEngine On 
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://www.erpsaa.com/$1 [R=301,L]

    The problem is now over 1,200 .php pages are all a mess because unlike when
    you redirect the entire site, unlike manually putting placing the .html pages
    into https, although it stays secure on all .html, it knocks off the s when you
    any .php page. That does not happen will a full site direct, it forces all the .php pages as well.

    I'm going to try this:

    RewriteEngine On
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://www.erpsaa.com/index.html$1 [R=301,L]

    Something tells me it won't work.


    Why is it so difficuly to find the answer with a search "301 redirect
    for pages with a specific extension only." Thanks for any help.

    Dean


     

     

     

     

  • Hello,

    This is my page url http://www.artocean.in/aboutus.php .. in this i want rewrite url i want add .html extenstion and remove .php extenstion.. but that is not working error occurs Not Found error occurs.

    And I want code for working on live server.. i am very tried to solve this issue…

    If anybody have solution then pleaseeeeeee share here..

     

    Thank you…

    • Hello Arpita,

      The problem with what you’re trying to do is exactly the error you’re running into – the file doesn’t exist. You should either create a re-direct to point to the .php page, or simply get RID of the extension altogether as per the article above. It’s better for your SEO as it becomes a much easier to read URL. We’re not sure what benefit you would get for using the HTML extension versus the PHP extension.

      Kindest regards,
      Arnel C.

  • Ah John-Paul you’re a legend!! How did I not see that? lol

    Thank you so much for not only getting back to me so quickly but for solving my problem, that will be fixed quite simply now that I can see what it is. Again, a million thank you-‘s ! 🙂

    • Hello sir, I have seen some results in Google search like “yourdomain.com > file > subfile”. While Google shows results for my website like “yourdomain.com/file/subfile”. How can i get Google search results like “yourdomain.com > file > subfile” ??

      My .htaccess is

      Options +FollowSymLinks
      Options -Multiviews
      RewriteEngine on

      ErrorDocument 404 /error404.php

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

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

      RewriteRule ^([^/.]+)/?$ /vendorlist.php?bcat=$1
      RewriteRule ^([^/.]+)/([^/.]+)/?$ /vendor.php?bcat=$1&vendor=$2

      Thanks in advance…

    • Hello AshishGarg,

      Thank you for your question. I am not quite understanding it, however. Would you be able to provide a link to an example of what you are asking? Are you asking to literally change the / to a > ?

      Kindest Regards,
      Scott M

    • Hello AshishGarg,

      Thank you for your question. I spent some time looking into this for you. Unfortunately, I do not know a way to change a “/” to a “>” symbol, as it may cause problems when the browser attempts to render the URL.

      Thank you,
      John-Paul

  • I’ve tried using the code above however for some strange reason it seems to work for my entire website with the exception of just 2 pages and I can’t find why?!?

    I’ve kept the .html at the end of the two pages not working for now just so the site is functional but to show you my problem I have created a duplicate of my home page at: http://www.riverinatradies.com.au/index2

    If you click on the directory link you will find a sub-menu that has wagga wagga albury wodonga and girffith. When clicked the wagga wagga link ads a trailing slash to the link and displays the page links instead of the page itself. In the same sub-menu, the Albury Wodonga links works perfectly fine and Griffith link has the same problem above.

    Any help you can provide would be greatly appreciated!! 🙂

    • Hello Libby,

      Thank you of your question. When I checked the “wagga wagga” link, I noticed it is just a link to a folder (directory-wagga), instead of a page. Since there is no index.html, index.php, or default.htm, it just lists the directories.

      I would compare the “wagga wagga” link, to the “Albury Wodonga” links, to see how they are coded. Most likely the “Albury Wodonga” link is coded differently, from the other two.

      I also recommend reviewing the existing .htaccess rules, as they may be interfering with this one.

      If you have any further questions, feel free to post them below.

      Thank you,
      John-Paul

  • Hi Arnel,

    Thank you so much for your reply. I had a look at that link and they say ‘works well except for pages that have 301 redirects.’

    So my message want posted very well.

    The removal of .php works fine with the multiview. We are concerned about incoming links going to old pages with .php and cant seem to redirect these.

    In the htaccess file this works

    Redirect 301 /files/Menu.pdf http://www.example.co.uk

    This works but puts a trail of the url onto the redirected url (which is ok for now)
    Redirect 301 /section.php/11/1/life-drawing http://www.example.co.uk

    This doesn't
    Redirect 301 /section/116/1/drawings http://www.example.co.uk

    Here are the contents of the htaccess file...

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^example.co.uk [NC] 
    RewriteRule ^(.*)$ http://www.example.co.uk/$1 [L,R=301]
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/ 
    RewriteRule ^index\.php$ http://www.example.co.uk/ [R=301,L] 

    RewriteEngine on
    Options +SymlinksIfOwnerMatch +MultiViews
    RewriteRule ^(.*).php/(.*) $1.php?$2

    Redirect 301 /files/GalvinCognacCigarMenu.pdf http://www.example.co.uk
    Redirect 301 /section.php/11/1/life-drawing http://www.example.co.uk
    Redirect 301 /section/116/1/drawings 
    http://www.example.co.uk

    is there any expert help i can pay for?
    Thanks
    Peter

    • Hello Peter,

      Sorry again for the problem with the re-direct. Unfortunately, we do not provide services to write the code for you. However, the link I initially provided states that THEIR code does not work well with 301 re-directs, but the reply includes a response that may have the solution to that problem. Since your 301 re-directs are not redirecting you may need to change them to not include the PHP extensions. If this does not resolve the issue for you, then you will need to seek a programmer for further assistance with that issue.

      You may also want to review the documentation provided y Apache for multiview for further possible clues on correcting the issue with your code.

      Regards,

      Arnel C.

  • Hi Guys,

    See you are resolving some issues here. Have a simiar problem with multiview being enabled to get rid of the the file extension .php globaly across the site, but now not all the 301 redirect’s work. We have lots of old inbound (good quality) links that need to go to new urls.

    In the htaccess file this works
    Redirect 301 /files/Menu.pdf http://www.example.co.uk

    This works but puts a trail of the url onto the redirected url
    Redirect 301 /section.php/11/1/life-drawing http://www.example.co.uk

    This doesn’t
    Redirect 301 /section/116/1/drawings http://www.example.co.uk

    Here are the contents of the htaccess file…

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^example.co.uk [NC]
    RewriteRule ^(.*)$ http://www.example.co.uk/$1 [L,R=301]
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
    RewriteRule ^index\.php$ http://www.example.co.uk/ [R=301,L]

    RewriteEngine on
    Options +SymlinksIfOwnerMatch +MultiViews
    RewriteRule ^(.*).php/(.*) $1.php?$2

    Redirect 301 /files/GalvinCognacCigarMenu.pdf http://www.example.co.uk
    Redirect 301 /section.php/11/1/life-drawing http://www.example.co.uk
    Redirect 301 /section/116/1/drawings http://www.example.co.uk

    Any help would be very much appreciated. 
    Thanks
    Peter

    • Hello P Garner,

      Sorry to hear that you’re having problems with the re-directs not working. It does appear however, that you will need to re-write your re-directs to take in account the removal of the PHP extensions. We can’t provide the re-write for you, but hopefully, looking at some examples from other discussions on this issue, you will find a solution. Check out this discussion as it seems to closely resemble the issue that you are facing.

      I hope this helps to answer your question, please let us know if you require any further assistance.

      Regards,
      Arnel C.

    • Hello Ashish,

      Thank you for contacting us. When I tested the “aboutus” page you linked to, I am not seeing a 404 error. Are you still experiencing issues at this time?

      You may have to clear your Browser Cache before testing again.

      Thank you,
      John-Paul

    • Hello Manish,

      Thank you for contacting us. What Arnel is suggesting is that you use the syntax provide in the article above for the .htaccess rules.

      This code has already been tested successfully, and works well.

      Thank you,
      John-Paul

      Thank you

  • Hi I have tried your code but it gives an error 404 when i follow address in addressbar. My website name is Ludhianaweds.com. When i type ludhianaweds.com/aboutus it gives an error 404. Here are two same files in my main directory with the names aboutus and aboutus.php.
    Any help ??
    Thanks in advance….

  • Helllo Arn,

    http://ravielectricals.in/ this is my other site which has same issue like not run with .html extenstion if i have add that in .htaccess file.

    my all pages save with .php but i want that all with .html extenstion.. so what can i do any changes perform on server??

     

    Thank you.. please reply

    • Hello Arpita,

      In visiting your site, I noticed it is running off of Microsoft IIS. We are not able to provide support for anything running off of a MS server as we do not use them. Our servers are all Linux based so any advice we give here is only based on those types of servers. We do not have any MS servers so we are unable to test and give correct advice.

      Kindest Regards,
      Scott M

    • Hello Arpita,

      For Apache servers the code to switch php to html should be something like this:

      RewriteEngine on
      RewriteBase /

      RewriteCond %{THE_REQUEST} (.*)\.php
      RewriteRule ^(.*)\.php $1.html [R=301,L]

      RewriteCond %{THE_REQUEST} (.*)\.html
      RewriteRule ^(.*)\.html $1.php [L]

      As I am not an expert in htaccess, this answer was sourced from stack overflow.

      Kindest Regards,
      Scott M

  • Hello Arn,

    I have to provide url in previous message but I send again this is my url http://artocean.in/servicecnt.php?service_id=3..

    and code is this

    Options +FollowSymLinks
    RewriteEngine on

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

    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^service_id=([0-9]*)$
    RewriteRule ^servicecnt\.php$ /art-ocean/servicecnt.php/%1? [R=302,L]

    i want remove query string from http://artocean.in/servicecnt.php?service_id=3 url and also add extestion .html in all page..

    .html extension is not working on live site.. but working on localhost perfectly so please help me..

    any change will be require on my server????

    Waiting for reply..

    Thank you..

     

  • Hello Scott,

    Htaccess file is not supporting on live server menas (client server) I add rule of remove .php extenstion that is perfectly work on my localhost but not working on live server..

    what is problem on that I dont have any Idea so please help me..

    and other issue is this http://artocean.in/ourservices.php first you click on this url and in that page 5 services will be displayed and that click on one of any services and that url is this http://artocean.in/servicecnt.php?service_id=3 in that url i want remove query string(?). and I also add rule for remove query string and result like that which I want but that page design is not displaying so what can I do..

    see below I attach one image about my above http://artocean.in/servicecnt.php?service_id=3 issue..

    Thank you..

     

    • Hello Arpita,

      Sorry you’re having problems with the redirects. However, you do need to provide us more information in order for us to help you with the re-direct issues you have indicated. Can you provide a us a URL where the issue is happening? And you can provide use the code that you are currently using? Please make sure to provide this information as well as any exact error messages you may be seeing.

      If you can provide us this information we can try to provide further assistance with the problem you are having.

      Regards,
      Arnel C.

  • Hello Scott,

    Thank you for supporting.. but my problem is solved.

    I use one plugin for that so problem is solved..

    thank you..

  • @Scott M
    Options +FollowSymLinks -MultiViews

    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /

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

    ## To internally redirect /dir/foo to /dir/foo.php
    RewriteCond %{REQUEST_FILENAME}.php -f

    RewriteRule ^ %{REQUEST_URI}.php [L]

    THANK YOU VERY VERY MUCH.. 200% working..after a couple of days search i finally found a working one.. TNX again

     

  • hello.

    in .htaccess file below page not access.

    when i click on my account link.

    RewriteRule MyAccount/$ ?do=my_account

    so. how i can solve this error ?

    thanks..

    • Various modules that change paths such as Patchauto can indeed affect how other rewrites on your site operate. This artice primarily applies to static files and not dynamically written URLs.

  • Hi! I’m experiencing the same problem: 404 error Page Not Found. I work under Drupal. The url shows correctly (i mean, without the extension), but i get a 404. Any idea? Thanks in advance.

    • Hello Arpita,

      I am not sure you can do that. I spoke with our WordPress guy and he says not to try and do that as it will cause many problems and require extensive coding to make it work properly.

      Kindest Regards,
      Scott M

  • Hello,

    When I add rule for remove query string then that my page design is not supporting what can I do??

    my url is this http://localhost/art-ocean/servicecnt.php/3

    and code is here..

    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^service_id=([0-9]*)$
    RewriteRule ^servicecnt\.php$ /art-ocean/servicecnt.php/%1? [R=302,L]

    is it correct..????

    url rewrite is correct but design issue.. 🙁

    and this code is not supporting on live server so i cant send you link..

     

    please reply me fast…

    • Hello Arpita,

      I am unsure what you are asking exactly. Could you give more details on what it is you are trying to accomplish?

      Also, if this does not relate to removing file extensions, please make it a new question.

      Kindest Regards,
      Scott M

    • The following line in your .htaccess file will allow you to remove the file extension from a single file:

      RewriteRule ^myfilename$ myfilename.html [L]

  • Hello Jeff,

     

    I tried below in .htaccess

     

    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /

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

     

    It does not take me to the page. My file name is mylogin.php. When I login it shows mywebsitename.com/mylogin but, throws error page not found.

    Appreciate your help.

     

    Thanks,

    Hemal

    • Hello Hemal,

      Sorry for the problem with code. Can you please reply with the code that is listed in your .htaccess file. Make sure the code is separated line by line – as your current copy of the code is unclear. We’re not sure how it is set in your .htaccess file without looking at your account.

      If you can provide us the exact code (or account information if you’re an InMotion customer), then we can investigate the issue further. We do not provide programming services, but we do try to point you in the right direction or provide a solution as long as we have the appropriate information.

      Kindest regards,
      Arnel C.

  • Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /

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

    ## To internally redirect /dir/foo to /dir/foo.php
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^ %{REQUEST_URI}.php [L]

     

    This code finally works… spent half a day finding it.

    I found other examples but I’d have two versions of the same file whereas with this code I get only one version… THANK YOU SO MUCH

    • Hello Arpita,

      Generally we ask that the comments and questions in an article relate to the article topic. You are more than welcome to ask a new question using the ‘Ask a Question‘ link. This way other people that have the same question can also find it easily.

      Kindest Regards,
      Scott M

    • Hello viralpatel,

      Unfortunately we do not have pdf files of our articles. However, I will submit an idea to our team on creating specific pdf guides for certain topics. Thank you for that idea!

      Kindest Regards,
      Scott M

    • Hello Traci,

      If you could reply with your exact code we are happy to test it on our server for you to see if it needs any tweaks.

      Kindest Regards,
      Scott M

    • Hello Viral,

      We are happy to take a look for you. Please reply with your code so we may see it.

      Kindest Regards,
      Scott M

  • Hi there.  I just did a copy and paste into the htaccess file and this code is not working.  

    Here is a link sample.  http://www.youreunique.net/index?page=our_difference

    I too, am a total newbie when it comes to webdesign.  My web guy was a friend and his mother passed away and he bascially disappeared.  We have already exhuasted our advertising/marketing/website budget for the year so any help that I can get will be great appreciated!  

    Traci 

     

     

  • I agree Scott. I doubt this will give any SEO or Security benefit. It is a cute trick someone thought up that I believe will be problematic at best, especially for larger websites that have been around awhile. Nice that someone actually said it, instead of just writing an article that appears to be 100% backed by the writer. Good job.

     

    Like you, I am seeing this more and more, but I can’t help but think it is a mistake. I would definitely not use the trailing slash because it represents a directory. What about all of the links already pointing to your pages, that you have no control over? They actually do have SEO value. They will turn into 404 errors that Google and other search engines do not like. I believe you have to change all of your internal links. CSS files and images might stop working. It just seems to be a huge risk, especially for higher traffic website.

     

    When is the last time you typed in a URL to a nested web file? Most use a link. Stores like Amazon do not do this and they dominate the web. My vote is for “NO!”. There are plenty of other things more worthy of your time. Link building, Article writing, Micro data, Accessibility to name just a few.

    • Article is excellent… and more helpful thing is that if we ask any question then we gave answer of them from your team so it is very helpful to us.

      If I have other query about php or wordpress etc them I will ask question in this article?????

      Thank you very much..

  • Hello Scott,

    I want a rewrite url in htaccess..

    I want open site with www and also without www so you have any idea about this so plzz share with me..

    Plzz help me..

    Thank You..

    • Hello Arpita,

      When working with WordPress, it will display in either www or non-www,whichever you choose. You can, however set the htaccess file to force either one you desire.

      A quick search in our search bar up top brought up our article on forcing www or non-www for your site.

      I hope that helps you,

      Kindest Regards,
      Scott M

  • Hello,

    I have littel problem in wordpress dynamic menu..

    My html page css is not working on wordpress dynamic menu so plzz help me..

    here i attach one image..

    You can here menu beside logo that menu is not proper space between 2 menu is not proper so i have to want make it proper so plzz help me…

     

    Thank You..

    Reply

    • Hello Arpita,

      Unfortunately I do not understand what you are asking. Did you create your own CSS and want it to work in WordPress? Did you modify existing css file in WordPress? Did you modify it in the WordPress back end?

      Also, this particular article is about htaccess files and file extensions, so it is likely better to ask a new question instead of placing it in a non-relevant article so other people can also help.

      And once again, images cannot be added to questions or comments. They are stripped out and do not display for anyone.

      Kindest Regards,
      Scott M

  • Hi scott,

    Its ok but my code is correct then it is not issue i think my server has a problem when I upload it on client server then problem will be solved..

    Thank you..

    • Hello Arpita,

      I do see that the services are leading to 404 errors. Unfortunately other than the code we have given we do not know what your server needs. We are unable to test because you are not one of our customers so we have no access to the server, nor its configuration. With the code working in localhost, you can tell it is generally correct. You may need to ask your host if there is anything else needed as they seem to have a configuration that is interfereing.

      Kindest Regards,
      Scott M

  • Hello Jacob,

    Here i send my actual page with design and without rewrite url..

    my design like this which i send you previous..

    thank you for reply..

    Reply

    • Hello Arpita,

      Unfortunately images posted into the comments are not allowed and stripped by our server. You will want to simply upload the images to your server and then place a URL in the comment to that image. Then we can take a look for you.

      Kindest Regards,
      Scott M

  • Hello Jacob,

    Thank you very much my url issue is solve i am soooo happy..

    but that url design will be changed it may not display..

    see i will send you..

    so now what can I do plzz give me solution..

    waiting for your reply..

    here I attach my page..

    • Hello arpita,

      Unfortunately your image wasn’t attached successfully. In the future if you could simply place any screenshots or images you’re trying to send us onto your website and then just provide a URL to them we can take a look for you.

      – Jacob

  • Hello Jacob,

    Any Idea about my issue then plzzz tell me..

    I am waiting for your reply..

    Thank you very much in advance..

    • Hello arpita,

      I don’t quite understand what you’re trying to accomplish with your rewrite rules, are you sure that this is the best method for the scripts you’re using? Perhaps if you could explain the purpose of your script it would be easier for us to recommend you something.

      When relying on rewrite rules to do something your scripts are handling, you can occasionally run into problems in different server environments. This sounds like the problem you are currently having trying to take your site live.

      For instance, if you wanted the URL http://localhost/art-ocean/servicecnt/4 to be a valid address. You could simply place a index.php file inside the directories /art-ocean/servicecnt/4.

      Typically you’d remove the file extension of URLs like this guide explains for SEO purposes and to make them more human friendly, but not much can be derived from /art-ocean/servicecnt.php/4 and it still includes the .php extension in the URL which is what this article talks about getting rid of.

      If you were getting a 404 error using my previous code example because it ends up redirecting you to:

      http://localhost/servicecnt.php/4

      Instead of going to where your script expects at:

      http://localhost/art-ocean/servicecnt.php/4

      Are you placing the .htaccess rules simply in your /public_html/.htaccess file? Or are you putting them inside the /public_html/art-ocean/.htaccess file?

      You might need to change it to include the sub-directory if you’re just placing the code in your main .htaccess file:

      RewriteEngine On
      RewriteCond %{QUERY_STRING} ^service_id=([0-9]*)$
      RewriteRule ^servicecnt\.php$ /art-ocean/servicecnt.php/%1? [R=302,L]

      Please let us know if you have any further questions.

      – Jacob

  • Hello JeffMa,

    You are right my code is not problem but my hosting server has problem i check that on another server it will run perfectly..so no any issue

    but i have another problem about remove query string from url

    here is my code:

    RewriteEngine on
    RewriteCond %{QUERY_STRING} ^service_id=([0-9]*)$
    RewriteRule ^servicecnt\.php$ http://artocean.omexinfotech.c... [R=302,L]

    code is work perfectly their output url is this:

    http://artocean.omexinfotech.com/servicecnt.php/3?service_id=3

    that is not correct i required only this not whole url

    url http://artocean.omexinfotech.com/servicecnt.php/3

    have idea to truncate last characters or any rule of that to remove so plzzzz suggest me…

    thank you..

    • Hello arpita,

      It looks like your RewriteRule got cut off so we can’t see the full code that you’re using. If you wanted someone going to this URL:

      http://example.com/servicecnt.php?service_id=5

      To end up at this URL:

      http://example.com/servicecnt.php/3

      You would use this code:

      RewriteEngine On
      RewriteCond %{QUERY_STRING} ^service_id=([0-9]*)$
      RewriteRule ^servicecnt\.php$ /servicecnt.php/%1? [R=302,L]

      The /servicecnt.php/%1? part of the RewriteRule should tell the server to redirect to /servicecnt.php/ followed by the service_id QUERY_STRING, and the ? at the end tells Apache to leave off that query string in the rewritten URL.

      Please let us know if you had any further questions at all.

      – Jacob

    • Hello Arpita,

      We’re sorry that you continue to have problems with the removal of the file extensions. The code we have offered works on our servers. It may be different on yours. You may want to check this tutorial from a third party, as it might give you working option.

      If it works for you, please let us know!

      Regards,
      Arnel C.

    • We have confirmed that the code itself does work, but it looks like you are hosted with someone else that may be causing issues on their end. I recommend working with your hosting provider for more information.

  • Hello,

    Thank you for a code but it is not working on localhost or live server..

    error occures 404 not found it is page not found error..

    plzz give me other examples..

     

    Thank you for reply..

  • Hello..

    I am working on one project i face one problem related htaccess file..it will run perfectly in localhost but same code is not working on live server in my website..

    here i smy code

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

    I am use this code its run perfectly but not on server i waste my most of time in that but no result are there so plzz help me..

    waitting for your reply what can i do for this problem.. 🙁

    Thank you in advance..

    • Hello Brenna,

      At this point you may want to check with your host as the above code is working on our servers. It is general code so it should work elsewhere but there could be something that is preventing it from working on the server on which you are located.

      Kindest Regards,
      Scott M

    • Hello,
      This artical is such a nice artical it will working on localhost very perfectly but when i test it on live server it is not working so plzzz help me..
      how can i improve my code give any hint or link of refrence site..
      thank u in advance..
      I am waiting your reply..

    • Hello Arpita,

      Please try the code below instead to see if it works for you:

      Options +FollowSymLinks -MultiViews
      # Turn mod_rewrite on
      RewriteEngine On
      RewriteBase /

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

      Kindest Regards,
      Scott M

      ## To internally redirect /dir/foo to /dir/foo.php
      RewriteCond %{REQUEST_FILENAME}.php -f
      RewriteRule ^ %{REQUEST_URI}.php [L]

    • Hello Brenna,

      You are correct, the code does not seem to work any longer. After more testing and experimenting with other sites recommendations (all of which did not work either), I finally found one that does work. I have tested it on my own server. Use this code below:

      Options +FollowSymLinks -MultiViews
      # Turn mod_rewrite on
      RewriteEngine On
      RewriteBase /

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

      ## To internally redirect /dir/foo to /dir/foo.php
      RewriteCond %{REQUEST_FILENAME}.php -f
      RewriteRule ^ %{REQUEST_URI}.php [L]

      Kindest Regards,
      Scott M

  • Oh, that makes sense. Sorry, I’m a complete newbie. What about for links? Is there any way I can direct from /brenna.php to just /brenna? I tried putting /brenna for a link and it displayed the not found error as well.

    • Hello Brenna,

      As far as the code for the link, you need to be specific (brenna.php, brenna.htm, etc) because the server is not so smart. The code in the .htaccess file should still take over and display it as example.com/brenna however so your visitors will never know the extension.

      Kindest Regards,
      Scott M

  • The url that is working is creamilla.com/brenna.php, however, when you type in creamilla.com/brenna, you get a not found error. The complete code in my .htaccess is:

    <Files .htaccess>
    order allow,deny
    deny from all
    </Files> 

    suPHP_ConfigPath /home/creamilla/public_html 

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

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

    Thank you.

    • Hello Brenna,

      Yes, that is correct, you will get a not found error. This is because the server needs to know what you are looking for specifically. Despite not showing the .php in the address bar, the file is still named brenna.php, there could also be brenna.htm and brenna.html file names, all being told not to display their extension. The server needs to know which one you want. In fact, if you do not put an extension at all, the server assumes you are looking for a subfolder named brenna. When visiting the site, either by code or typing directly, you must be specific. The removal of the extension is simply for aesthetics.

      Kindest Regards,
      Scott M

    • Brenna,

      Apologies for the problem with the .htaccess. However, without any information on your website, it makes it difficult for us to help troubleshoot the problem. Can you please provide a URL and a description of how we duplicate the problem you’re seeing?

      Kindest regards,
      Arnel C.

  • Hi there! A .htaccess file was not in my file manager, even after making sure I would be able to see hidden files, so I had to create one on my own. I am trying to remove extensions from my website using the two codes listed above (for .html and .php) and it is not working. This is the code I have:

     

    Options +FollowSymLinks

    Options +Indexes

    RewriteEngine on

    RewriteCond %{SCRIPT_FILENAME} !-d

    RewriteRule ^([^.]+)$ $1.php [NC,L]

     

    RewriteEngine on 

    RewriteCond %{REQUEST_FILENAME} !-d 

    RewriteCond %{REQUEST_FILENAME}\.html -f 

    RewriteRule ^(.*)$ $1.html

    • Hello Brenna,

      Sorry for the problems with the .htaccess file. In reviewing the code you provided, it does not match what was provided in the article above. Please go back and edit your .htaccess file so that it matches what Scott has provided for the re-write rules and it should work.

      If you continue to have problems, please let us know.

      Regards,
      Arnel C.

  • Hi Vijay, you’re probably referring to a linked page if you’re still seeing the file extension. Make sure you’re links don’t have .html or .php at the end of them:

    <a href=”linkedpage.php”>link text</a> is wrong, it will force the browser to display .php despite the rewrite engine in use.

    <a href=”linkedpage”>link text</a> is right, this will ask the browser to find this file, and with the rewrite engine it will find it and display it without the extension.

    • Hello Vijay,

      We would be happy to assist you. Would you please reply with the code you have in your .htaccess so we can take a look?

      Kindest Regards,
      Scott M

Leave a Reply to USER Cancel reply