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
- Log into your cPanel.
Find the Files category and click on the File Manager icon.
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.
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.
- 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.phpRewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html - 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.
The problem i’m having with everyone supplying these codes to use is that no one has yet to show or explain exactly where in your htaccess file should you place the code that you’re providing. For a beginner like me who knows absolutely nothing this can be difficult to understand. I’ve placed the code in just about every place in my file and it gives me an error or my content completely disappears. Is there somewhere I can find exactly where to place this line of code within the code?
Storch2020, In general if you’re trying to add code that affects the navigation of your site, then I would recommend it go to the top of the file unless you have redirects that affect the file type or directories that you are working on.
Thank you. It successfully load www.example.com/abc but if I type www.example.com/abc.html ,it is loading with html extension. I do not need to load with this .html. Please help me.. Searching for days ..and didn’t get any solution.
Have you cleared all server caching software? If so, try adding this:
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
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
Works fine for me (while many other suggestions did not). Thanks
hi, i want my files extension will be hide in url i used your code isnot working please help me.
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.
when i want to open my website https://kalpyog.in/
its open https://kalpyog.in/index.php/ its showing enteronal error. i want my website by default open in https://kalpyog.in/index.html
Best Regards
Nishant Rai
In order to select which file is used by default for your website, you can specify this in your .htaccess file. Check out our guide How to Change your Default Index Page in .htaccess for instructions. I hope this helps!
Sincerely,
Carlos D
At least I got the weapons for a killing the .php
and thanks to InMotionFans for wrote this code.
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
What is the problem and what are you trying to do?
Still the extention is diaplayig in url
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..https://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:-
https://www.skytecsolution.com/about.html
https://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…
Did you follow the steps in the post?
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 justfile
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, 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’.
hello guys.
i have problem that its hard to find an answer.
guys. how to remove the ?i=1 from my url using .htaccess.
example www.myDomain.com/?i=1 and i want to remove the ?i=1 to the end of my url using .htaccess.
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.
Dear Scott,
It worked like a charm. Thanks a lot.
I am creating Maven Project my url run as ” https://localhost:8080/FlowerWebsite/occassions/flowers.jsp?cn=QW5uaXZlcnNhcnk=” that convert as “https://localhost:8080/FlowerWebsite/occassions/flowers?cn=QW5uaXZlcnNhcnk=”
I need this type url string flowers.jsp= flowers
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
Hello user,
What is it specifically you are wanting to do?
Kindest Regards,
Scott M
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.
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.
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
Hi,
I want to change my URL https://nn7.tv/play_user_video?title=5-Awkward-Moments-During-Exam—Funny-Video&tableName=user_videos&videoId=3
into
https://nn7.tv/play_user_video/5-Awkward-Moments-During-Exam—Funny-Video
How Can i do that? Please HELP
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
Good evening
I have got three domains and unfortunately in all the three domains i always get the same error
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
can i get a suggestion how to remove this .htaccess error.
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!
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..
I want to change this url
testelza.com/matrimony/forget_pass.php?login=1&err=1
to
testelza.com/matrimony/forget_pass/1
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.
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
how can ireplace all the .html extension to .html#Details in .htaccess
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
Ok thanks now i get it
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
Hallo
Guys i failed to remove this html extension on my url can you guys help me out
please help, i need all info on what to write on my .htaccess
Hello Bibi,
Can you please provide the code you used to try and remove the files extensions?
Best Regards,
TJ Edens
Thanks a lot, very very helpful compared to the thousand of answers on google.
Hi, how would I put the code in for removing the .php AND the .html file extension, since I use both..? Thanks!!
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.
how i can remove hyperlink from my website hold page please advice asap
thanks
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$ https://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.
Nice Posting Sir
Thanks Alot with my hearts
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.
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 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
Hello Arnel,
Thank you for replying but my problem is solved…
Thank you again for future support…
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: https://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:
Hello,
You could trying doing something like the following.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/folder/$1 [R,L]
So pretty much this means, for any .htm or .html file that is accessed on port 80, redirect to ssl version.
Best Regards,
TJ Edens
Thank you so much! c: This is very helpful. I followed the instructions above and your help instructions to the other dude.
Hello,
This is my page url https://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
Absolutely Scott Sir…..I want to change “/ to a >” in Google search results…Thanks
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
nice and so helpful
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: https://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 https://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 https://www.example.co.uk
This doesn't
Redirect 301 /section/116/1/drawings https://www.example.co.uk
Here are the contents of the htaccess file...
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.co.uk [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ https://www.example.co.uk/ [R=301,L]
RewriteEngine on
Options +SymlinksIfOwnerMatch +MultiViews
RewriteRule ^(.*).php/(.*) $1.php?$2
Redirect 301 /files/GalvinCognacCigarMenu.pdf https://www.example.co.uk
Redirect 301 /section.php/11/1/life-drawing https://www.example.co.uk
Redirect 301 /section/116/1/drawings
https://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 https://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 https://www.example.co.uk
This doesn’t
Redirect 301 /section/116/1/drawings https://www.example.co.uk
Here are the contents of the htaccess file…
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.co.uk [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ https://www.example.co.uk/ [R=301,L]
RewriteEngine on
Options +SymlinksIfOwnerMatch +MultiViews
RewriteRule ^(.*).php/(.*) $1.php?$2
Redirect 301 /files/GalvinCognacCigarMenu.pdf https://www.example.co.uk
Redirect 301 /section.php/11/1/life-drawing https://www.example.co.uk
Redirect 301 /section/116/1/drawings https://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.
this code only run on localhost. but not working in live.
so pls tell about. and fix solutions.
Thanks
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
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
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,
https://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:
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 https://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 https://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 https://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 https://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 https://artocean.in/servicecnt.php?service_id=3 issue..
Thank you..
Hello pradip,
What is it you are trying to accomplish?
Kindest Regards,
Scott M
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..
Yes it does! May it be because of some problem with Pathauto or another module like that?
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 Munir,
Does it work without Drupal on a test php file?
Kindest Regards,
Scott M
Hello,
How to add .html extenstion in wordpress..
please reply..
Could you clarify what specific design issues you are seeing? Could you provide a screenshot?
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 https://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,
This is my site url https://artocean.in/ourservices.php in that i want url dynamic means when I click on any of the services at that time my url is same like this https://artocean.in/servicecnt.php?service_id=4 but I want a url like this https://artocean.in/printing.php?service_id=4 is it possible and possible then how it is possible..
plzzzz help me of this issue..
thank you in advance..
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
i have to remove .html from only one url,plz help me
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.
thank u for this coding…..this code is very usefull for hiding the extension in my site thank u…..
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
using this code i am not able to remove the extension from my code.
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
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
Hi there. I just did a copy and paste into the htaccess file and this code is not working.
Here is a link sample. https://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..
pls send me a pdf for this….
Hello Scott,
Thank you very much for reply..
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 Scott,
Thamk you for reply..
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 Jacob,
Here is my url https://artocean.omexinfotech.com/ourservices.php which design is not supported when I rewrite query string url..
Please check it and give me suggestion..
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 https://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:
https://localhost/servicecnt.php/4
Instead of going to where your script expects at:
https://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 Jacob,
Your code nice but… url is not proper or 404 vpage not fount error is genrated when i use your code..
https://localhost/servicecnt.php/4 like this in this url my project folder name is missing so url is not found..
https://localhost/art-ocean/servicecnt.php/4 my actual url is this when this url meet then no error occurs..
so give me modification in that code which is provide by you..
waititng for your reply..
Thank you..
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:
code is work perfectly their output url is this:
https://artocean.omexinfotech.com/servicecnt.php/3?service_id=3
that is not correct i required only this not whole url
url https://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:
https://example.com/servicecnt.php?service_id=5
To end up at this URL:
https://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 JeffMa,
Yes ofcourse my url is this https://artocean.omexinfotech.com/index.php..
in this url i have problem this is run perfectly in localhost but problem is on live site..
plzz check it and give me suddestion..
thank you..
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..
Could you provide me with the URL that you are having trouble with?
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..
I entered that code into my .htaccess and the .php still shows when I click the link.
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:
# 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]
Thank for for the suggestion, Jeff. However, it doesn`t seem to have cleared up the problem.
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:
# 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
I put the .php at the end of the address in the link, however when I check it, it still goes to /brenna.php.
You may have old data cached in your browser. Try clearing your browser cache.
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
I have edited my .htaccess file with the code above and it still does not work.
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.
dear sir there is a code utf-8 and page showing empity
thanks
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.
using this code i am not able to remove the extension from my code..
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