The following may be considered over the top, but I like to take it one step further by adding a trailing slash. If the slash is missing I feel like something is left… unfinished? Just a personal quirk I guess.
This rule will match site.com/path/ and check to see if site.com/path.php exists. If the file exists it proceeds with the rewrite.
This may seem rather noobish, but if you want a slash after http://example.com/about, you could just as well do http://example.com/about/index.html which is much easier.
But I guess you have a specific situation which allows for your .htaccess approach =]
mod rewrite works only on apache server.
before trying please be sure that you are working on apache server and the mod rewrite module/extension is enabled.
I am unable to see any change to my site URL’s. How can I changed that. When i visit I found every time .html extention. Please give me a easy solution for solve this… I am waiting for expert reply…. Thank You
You did not mention that how we will apply this code..! for this i will guide…
we will make one file with the name of (.htaccess) and after that will paste this code.
A simple thing: This rule is inserted in an Apache Server .htaccess file. If you don’t have this file, you must create it. And this requires your host has activated the rewrite module.
Hi! I have a question about URL rewrite. I use a dynamical php file to load my posts as usual and I use the rewrite in the form: mydomain.com/id_title-of-the-post , so as you can guess, the real URL is something like mydomain.com/file.php?id=XX , and I look in my database for the post with id=XX.
In that case, the only real thing that matters in the URL is the “id” part. You could type mydomain.com/id_another-stupid title-of-the-post, and you will get to the same post.
But I’ve seen that, like in your website, your URLs are in the form css-tricks.com/category/title-of-the-post ….you don’t use an ID anywhere so the only thing I can think right now is that your DB query looks for the post where title field=title of the post.
Am I right or i’m talking BS? I can make that query too but I thought that looking for just an ID is way much faster for the database.
This is all under the assumption that you also use a dynamical file that loads your posts and each one is not an independent html file.
I would really appreciate if you can shed me some light on that matter. Thank you very much! (I really like your web ^^). Greetings from Peru =]
Hello Chris and everyone.
My first post here. Thank you for all the great tips! I’d like to share my favorite version of the script. It does not add a trailing slash unless you visit a index file. Remember to remove the “.php” from your href links.
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
A question. I use the trick and works perfect with files in the main root
RewriteRule ^about$ about.php [L]
my problem is with subdirectory files
RewriteRule ^/path/about$ /path/about.php [L]
can someone help me to use the trick?
Unless you are using a single access point you will likely need a .htaccess file in each directory to specify what that directories rewrite conditions are.
I just wanted to say that the comment “Remember to remove the “.php” from your href links.” was the magical ray of sunshine that got this to work for me! I hadn’t done this at first, but now this works wonderfully!
This original suggestion works well but means you need to list each page to have its extension removed, which may become quite tedious so I tried the one suggested by Doug which works and means it can be sitewide rather than specifying each page. The one suggested by Boris works too and have 1 less line of code so I am using that one.
So, the questions is: Is there any way to combine this sitewide code with a 301 to automatically redirect all requests to their extensionless url???
P.S. Really great site in general and loving the htaccess tips.
Sorry, my english is too bad, so i use Google translate: “For it to work you must make sure that in the <head> of your page you have the meta-tag <base href = "/" /> and that the href points to the root of your site…”
I’m sorry I gave that bad advise in the earlier post – to remove .php from href links. I was young and stupid.
I have a better solution. I’ve decided to go a bit in details for those who need it. Take a look and read on.
# Apache Rewrite Rules
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
# End of Apache Rewrite Rules
</IfModule>
Things I guess you want to know:
1. This is for sites that don’t use a front-end-controller where all requests are forced through index.php. If you need that, you must replace the third block with your own.
2. The example is showed with .php only. If you want to use it with .html for example, you must copy/paste the third block and replace “.php” on line (1,) 3 and 4 with “.html” (unless someone has a neat way of doing it?)
3. The content of “http://example.com/something/” could be served from either “http://example.com/something.php” or “http://example.com/something/index.php”.
4. To link to other pages, just href=”/some/thing/” or href=”http://example.com/some/thing/”.
5. With .css and .js you want to href=”/style.css” or href=”/folder1/style.css”. Else if you href=”style.css” from “http://example.com/folder2/file.php” it’ll look at “http://example.com/folder2/style.css”.
@Boris: I’m using your solution on my site, and I find that it works great for one level. Thanks very much! But how do I use it for a site with multiple levels? If I set up example.com/contact.html, then example.com/contact/ will go to that page. But if I create a directory called contact/ and try to add contact/thanks.html (I want to have example.com/contact/thanks/), then going to example.com/contact only gives me the listing of the contact directory instead of contact.html. How do I fix this? (Or is a different hierarchy better?)
This works really well to add the trailing slash back in, but what if you want to put some variables in your URL? So for example, if I wanted example.com/something/variable_for_something_template where something is actually “something.php” and variable_for_something_template is a value that the something.php template looks for?
Looks like you got it working, perhaps without knowing. I went to your site and the pages loaded with the .html extension, but when I removed the .html, it loaded the same page.
These rewrite rules don’t mean the page won’t stop loading under the old name, but it just makes the other name work as well. I imagine there is a way to redirect the .html to remove the extension, but I’m not sure how and I imagine there is some fancy trick to it.
i have rewrite my forum .php into .html like this ;
RewriteEngine On
RewriteBase /forum
RewriteRule ^(.*)_([0-9]+).html$ index.php?id=$2 [L]
RewriteRule ^(.*)_([0-9]+)_p([0-9]+).html$ index.php?id=$2&page=$3 [L]
RewriteRule ^(.*)_p([0-9]+).html$ index.php?act=post&id=$2 [L]
RewriteRule ^(.*)_([0-9]+)_s([0-9]+).html$ index.php?id=$2&start=$3 [L]
RewriteRule ^(.*)_([0-9]+)_clip_p([0-9]+).html$ index.php?id=$2&clip&page=$3 [L]
anyone can help me to remove .html etension?? please,,,
Hi All,
I’m trying to rewrite http://www.example.com/services.php?m=services&s=scan book&id=2 to http://www.example.com/services/services/scan-book/2 . I tried many ways and also tested above examples. But it is not ok yet. I encountered the following error.
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, [email protected] and so on ….
Can any body help me solve my problem please?
I am not sure if this is always the case but try getting rid of .txt from the file name, it should just be .htaccess and be aware that your OS (like on my Mac) may hide files that start with a ” . ”
If push comes to shove, I name it without a dot, and perhaps with a .txt file type and then rename it once it has been uploaded to my server.
I am new in creating htaccess code and i have a problem whit it, which is very annoying.
I am using the code written by Boris which could be perfect to me. My only problem is that when I click on the following link: http://mydomain.com/login/ or http://mydomain/login (which is actually http://mydomain.com/login.php) it redirects perfectly, but my href becomes as follows: mydomain.com/login/login.
As I understand the last slash is considered to be the root so the program appends the url to it, but i don’t want to. How can I avoid this phenomena, what is my problem?
I have created the .htaccess file on my site and pasted in the coding listed above. Nothing. I use Godaddy, if that means anything. I also use Smultron to code my site.
The URL gets changed to what i want perfectly. But the page remains the same.
For example,
Suppose i m on page1.php. When i click on menu ‘page2’ (page2),
the url gets changed to “domainname/dir/?page2” but browser dont show the ‘page2’ contents.
I am not using any CMS. Its just a simple php website.
Hello Niall…
I tried your code..
changed php to html in the code
however only ONE link is working from all links on the page.. others are showing “404” or access forbidden
I also added
Options -MultiViews as I am hosting with 1and1.com
below is exact code I have used in the .htaccess for your reference
Options -MultiViews
AddType text/x-component .htc
RewriteEngine On
RewriteBase /organic-herbal
remove .php; use THE_REQUEST to prevent infinite loops
FYI the above can be dropped into any Apache server and you can reference example.php or /example folders will also render correctly but dont put a file named foo.php and a folder called foo, Apache will get upset :)
I’ve tested 25 different solutions, and only this one makes my morning happy. Thank you Naill. And now I wonder – is it really needed for SEO, or it is just …. ?
Yeah it is my baby, been working on it for a while, it does help with SEO, if anything I think it just makes sense not to show a file extension to a normal brochure audience.
I have tried around 15 different codes to try and make the page load without .php at the end but none of them work.
I have contacted my host and they advised that their is nothing on the server which should stop a mod rewrite.
This is the latest one I have attempted. Do I need the file in a certain format. I downloaded the original blank htaccess file and opened it in Wordpad.
I’m having issue getting RewriteRule to work on my site. I’ve used the following code, but it doesn’t seem to be working. I really don’t know how to get it to remove the .php when the page is loaded.
Hi Niall, I tried the code it works, but does the following
1) when i put my mouse on the link for example on “About us” and look down it is showing aboutus.php, but when i click it it will display http://www.mysite/aboutus with out the a forward slash on the front.
2) I don’t want people to see the .php when they place their cursor on the link before clicking
I would use the following for testing this http://htaccess.madewithlove.be I can help you with this but best you get a nice test environment as I hate giving ppl 500 errors :)
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Worked fine for me, It looks like a lot of people are not realizing that in order for this to work, you need to navigate to the new URL without the extension ie . http://www.domain/help.php , you would browse to http://www.domain/help and the browser will load help.php…. it wont magically remove the .php extension if that is how you have typed in the URL or linked to the page with a href containing the .php extension.
I have a website with wordpress blog. I have removed the php extension of all files in the root folder using htaccess. when i access http://www.mydomain.com/blog, it redirects to 404 page. I created a htaccess inside wordpress folder with Rewrite engine off but no use .
Hi! I am having a blogger and like other website’s cPanel, I am unable to find the .htaccess file which is in hidden shape. Please let me know that “How could I find the .htaccess file” in the root directory of my blogger, or I may allow, if created a new one .htaccess file for my blogger. Kindly solve my problem. Thanks and Best Regards for all the Readers.
I am working on one website..I have a problem in .htaccess file when i hide url .php its work on localhost perfectly but when i upload on live site or server it may get error 404 file or directory not found…
I will change all the thing like http.conf mod enable and all that.. but no result found
my lots of time waste on this topic but no any output so plzzzz help me..
thank you in advance..
I am waiting for yours reply…
I don’t know about this. I would think removing your extensions would be problematic at best. 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. Maybe for a small site or blog, but for a large website it seems to carry too much risk. What about all of the links already pointing to your pages? 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 for higher traffic website, especially.
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. I question whether there is really any SEO value.
IIS Servers dont use htaccess, it is an apache file for generally PHP settings unless I ma missing something. Search for IIS or ASPX re-write scripts should help :)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html
I have tried everything to no avail even putting html files in a sub directory and re-naming it index.html in a folder for eg:http://stuartmansfield.com/bio/index.html bio folder I am using dreamweaver cs6 fluidGrid layout html5 I think Go Daddy has no clue anyone know how? I tried all of the above i am at my whits-end.
In case any newcomers are having trouble with this. You have to remove all .html links from your source code for RewriteCond to work. This is easily done in Dreamweaver for anybody using it. Just use the ‘find’ function and type .html for your entire website and ‘replace all’ with nothing.
how do i remove/hide the .html on my url? im using adobe muse and it’s hosted by web.com. please help, i tried all the codes listed here and none made any changes to the url.
I used a few Adobe CMS before never a huge fan, even at enterprise level, but thats just my opinion. Equally this and things like Kentico which I bundle in the ‘awkward to do normal stuff in’ bracket can normally be found in the CMS.
I’ve used htaccess for other redirects, however, I’m confused about removing extensions and have a few questions.
If you manually remove the extension from a file name then you can’t view the page on your computer (I write code by hand so don’t use a CMS, etc). I tried setting up a site with MAMP (never again).
If you load the pages with .html extensions and remove the html with htaccess so it gets indexed without the .html I assume all outside links need the .html removed. Yes?
Also, I don’t see how this redirect hides anything as anyone can view the source code.
It’s, somehow, the matter of beauty (called “Pretty URLs” – which are more understandable); And not only for .html files! for all kind of files and most of them are not .html files, are -ugly- PHP/ASP files!
Also, you can do more advanced things like converting a query string to a plain URl.
Isn’t it nice to have:
“example.com/sample/anothersample1/anothersample2”
instead of long/ugly
“example.com/sample.php?longquery=anothersample1&anotherlongerquery=anothersample2”
(Off-course this is not mentioned here and needs some work and mind)
The following may be considered over the top, but I like to take it one step further by adding a trailing slash. If the slash is missing I feel like something is left… unfinished? Just a personal quirk I guess.
This rule will match site.com/path/ and check to see if site.com/path.php exists. If the file exists it proceeds with the rewrite.
RewriteCond /%{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.php
This may seem rather noobish, but if you want a slash after http://example.com/about, you could just as well do http://example.com/about/index.html which is much easier.
But I guess you have a specific situation which allows for your .htaccess approach =]
That kinda defeats the purpose though, doesn’t it? The idea is to remove all those ugly file extensions and tech words from your URLs.
I think @bram meant that http://example.com/about/ will resolve to http://example.com/about/index.html (without showing the index.html part).
Instead of RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.php you could do RewriteRule ^(.*)/$ /$1.php
Greg, thank you for your post here. It rocks.
This one seems a bit easier:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
This version breaks any urls with a dash ‘-‘ between words.
Although that said Greg’s seems to as well…
I’ve never encountered that problem. In fact, I just tried it and it worked fine.
Having tried all the examples above,
RewriteRule ^about$ about.php [L]
&
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
I am unable to see any change to my site URL’s.
The suffix is still visible when I reload the page.
Any ideas on how I can correct this?
mod rewrite works only on apache server.
before trying please be sure that you are working on apache server and the mod rewrite module/extension is enabled.
@Chris Radford
Hello Chris Radford, there may be some server problem with you or you might not using apache.
I have used the above code and it works well for my website.
http://jayjalaramext.com/brass-section-profile opens http://jayjalaramext.com/brass-section-profile.php
Thanks
that works nice
I am unable to see any change to my site URL’s. How can I changed that. When i visit I found every time .html extention. Please give me a easy solution for solve this… I am waiting for expert reply…. Thank You
Thanks Dyllon.
Dear Dyllon,
You did not mention that how we will apply this code..! for this i will guide…
we will make one file with the name of (.htaccess) and after that will paste this code.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
and save it in the root of the site.
and after that one by one we will change the link, eg. about_us.php to about_us and so on.
keep enjoying the programming.
Thanks.
Hi every one
for remove .html
after try many different ways This worked for me . in .htaccess root directory :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $.html [L]
i would like this snippet, but how can use this snippet? where i use it? anyone explain this deeply?
put it in your .htaccess file in the directory where you want the changes to take affect
A simple thing: This rule is inserted in an Apache Server .htaccess file. If you don’t have this file, you must create it. And this requires your host has activated the rewrite module.
Hi! I have a question about URL rewrite. I use a dynamical php file to load my posts as usual and I use the rewrite in the form: mydomain.com/id_title-of-the-post , so as you can guess, the real URL is something like mydomain.com/file.php?id=XX , and I look in my database for the post with id=XX.
In that case, the only real thing that matters in the URL is the “id” part. You could type mydomain.com/id_another-stupid title-of-the-post, and you will get to the same post.
But I’ve seen that, like in your website, your URLs are in the form css-tricks.com/category/title-of-the-post ….you don’t use an ID anywhere so the only thing I can think right now is that your DB query looks for the post where title field=title of the post.
Am I right or i’m talking BS? I can make that query too but I thought that looking for just an ID is way much faster for the database.
This is all under the assumption that you also use a dynamical file that loads your posts and each one is not an independent html file.
I would really appreciate if you can shed me some light on that matter. Thank you very much! (I really like your web ^^). Greetings from Peru =]
This is awesome, I had no idea you could do that. Will be doing it on my sites later on :)
Dyllons method worked for me. Thanks for that, been looking around and no other method that I found worked for me.
Hello Chris and everyone.
My first post here. Thank you for all the great tips! I’d like to share my favorite version of the script. It does not add a trailing slash unless you visit a index file. Remember to remove the “.php” from your href links.
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Hi everyone
A question. I use the trick and works perfect with files in the main root
RewriteRule ^about$ about.php [L]
my problem is with subdirectory files
RewriteRule ^/path/about$ /path/about.php [L]
can someone help me to use the trick?
Hi
Remove File Extention from URLs is not working in my site.. Can you give some tips to solve the proble
Unless you are using a single access point you will likely need a .htaccess file in each directory to specify what that directories rewrite conditions are.
Using this snippet as an example, you would place this .htaccess file both the http://www.example.com/about directory as well as http://www.example.com/about/sub directory.
What would the code be if it were to remove both .php and .html from the url?
I just wanted to say that the comment “Remember to remove the “.php” from your href links.” was the magical ray of sunshine that got this to work for me! I hadn’t done this at first, but now this works wonderfully!
This original suggestion works well but means you need to list each page to have its extension removed, which may become quite tedious so I tried the one suggested by Doug which works and means it can be sitewide rather than specifying each page. The one suggested by Boris works too and have 1 less line of code so I am using that one.
So, the questions is: Is there any way to combine this sitewide code with a 301 to automatically redirect all requests to their extensionless url???
P.S. Really great site in general and loving the htaccess tips.
yeah changing extension in all the page is tedious please can u send the code which u used for one time
@Richard Ellis: If your website’s extensions are anything other than “.php”, then you have to change the .”php” to “.html” in the code you copy.
The code worked, but now it removed all my css when you go straight to http://www.whitebluffsbrewing.com/news – Any suggestions?
Sorry, my english is too bad, so i use Google translate: “For it to work you must make sure that in the
<head>
of your page you have the meta-tag<base href = "/" />
and that the href points to the root of your site…”Thanks ! That worked for me !!!!
Hello again.
I’m sorry I gave that bad advise in the earlier post – to remove .php from href links. I was young and stupid.
I have a better solution. I’ve decided to go a bit in details for those who need it. Take a look and read on.
Things I guess you want to know:
1. This is for sites that don’t use a front-end-controller where all requests are forced through index.php. If you need that, you must replace the third block with your own.
2. The example is showed with .php only. If you want to use it with .html for example, you must copy/paste the third block and replace “.php” on line (1,) 3 and 4 with “.html” (unless someone has a neat way of doing it?)
3. The content of “http://example.com/something/” could be served from either “http://example.com/something.php” or “http://example.com/something/index.php”.
4. To link to other pages, just href=”/some/thing/” or href=”http://example.com/some/thing/”.
5. With .css and .js you want to href=”/style.css” or href=”/folder1/style.css”. Else if you href=”style.css” from “http://example.com/folder2/file.php” it’ll look at “http://example.com/folder2/style.css”.
Boris
@Boris: I’m using your solution on my site, and I find that it works great for one level. Thanks very much! But how do I use it for a site with multiple levels? If I set up example.com/contact.html, then example.com/contact/ will go to that page. But if I create a directory called contact/ and try to add contact/thanks.html (I want to have example.com/contact/thanks/), then going to example.com/contact only gives me the listing of the contact directory instead of contact.html. How do I fix this? (Or is a different hierarchy better?)
This works really well to add the trailing slash back in, but what if you want to put some variables in your URL? So for example, if I wanted example.com/something/variable_for_something_template where something is actually “something.php” and variable_for_something_template is a value that the something.php template looks for?
Great! i used your code and it worked on my site, but its not picking the CSS and Js file
What do i do?
Thanks, Boris. Your solution worked perfectly for me! :)
Hi All,
I’ve been trying to remove the .html file extension from my site.
I’ve uploaded a .htaccess file to my root directory, with this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html
Nothing is happening.
I previously tried this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
As well, and got an internal server error on all of my pages.
Any help would be greatly appreciated…the above comments mostly had to do with removing .php so I didn’t know if those would apply.
Thank you!
Looks like you got it working, perhaps without knowing. I went to your site and the pages loaded with the .html extension, but when I removed the .html, it loaded the same page.
These rewrite rules don’t mean the page won’t stop loading under the old name, but it just makes the other name work as well. I imagine there is a way to redirect the .html to remove the extension, but I’m not sure how and I imagine there is some fancy trick to it.
Hope I helped.
change the permission in your file manager on server to “644” or “777” maybe work
Hey great post and interesting topic. Thanks for sharing.
I found Boris’ last submission to be the best option.
Anyone tried this while using ajax to inject the main content ? wondering if that has something to do with my lack of success… ?
i tried this in .htaccess file but its not working can you please check this
i have rewrite my forum .php into .html like this ;
RewriteEngine On
RewriteBase /forum
RewriteRule ^(.*)_([0-9]+).html$ index.php?id=$2 [L]
RewriteRule ^(.*)_([0-9]+)_p([0-9]+).html$ index.php?id=$2&page=$3 [L]
RewriteRule ^(.*)_p([0-9]+).html$ index.php?act=post&id=$2 [L]
RewriteRule ^(.*)_([0-9]+)_s([0-9]+).html$ index.php?id=$2&start=$3 [L]
RewriteRule ^(.*)_([0-9]+)_clip_p([0-9]+).html$ index.php?id=$2&clip&page=$3 [L]
anyone can help me to remove .html etension?? please,,,
Hi All,
I’m trying to rewrite http://www.example.com/services.php?m=services&s=scan book&id=2 to http://www.example.com/services/services/scan-book/2 . I tried many ways and also tested above examples. But it is not ok yet. I encountered the following error.
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, [email protected] and so on ….
Can any body help me solve my problem please?
Many Thanks
Hi All,
I am trying to rewrite the URL http://www.example.com.br/folder/sub_folder/my_profile.php to
http://www.example.com.br/folder/my-profile. I have tried lot of rewrite method but i can’t redirect the URL.
Please anyone help to solve this.
i tried in many ways in .htaccess.txt file. But still no change. I think the issue may be some what different. Can anyone guess the issue?
I am not sure if this is always the case but try getting rid of .txt from the file name, it should just be .htaccess and be aware that your OS (like on my Mac) may hide files that start with a ” . ”
If push comes to shove, I name it without a dot, and perhaps with a .txt file type and then rename it once it has been uploaded to my server.
Hi Kumar,
I hope this may helps you,
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
Its working for me..
404 page not found error genreted… plzz. suggestion me.. plzzz.
hi
i am use below code but it’s not work in my localhost so please help me.
RewriteEngine on
RewriteBase /test/
RewriteRule ^enquiry$ enquiry.html [L]
Hi,
I want to modified url in my site same as WordPress
I have: http://mysite.com/5-remove-file-extention-from-urls.html
How to rewrite to: http://mysite.com/remove-file-extention-from-urls
htaccess
RewriteRule ^page,([0-9]+),([0-9]+),([0-9]+)-(.).html(/?)+$ index.php?newsid=$3&news_page=$1&cstart=$2&seourl=$4 [L]
RewriteRule ^page,([0-9]+),([0-9]+)-(.).html(/?)+$ index.php?newsid=$2&news_page=$1&seourl=$3 [L]
RewriteRule ^print:page,([0-9]+),([0-9]+)-(.).html(/?)+$ engine/print.php?news_page=$1&newsid=$2&seourl=$3 [L]
RewriteRule ^([0-9]+)-(.).html(/?)+$ index.php?newsid=$1&seourl=$2 [L]
Thanks.
Hello everyone,
I am new in creating htaccess code and i have a problem whit it, which is very annoying.
I am using the code written by Boris which could be perfect to me. My only problem is that when I click on the following link: http://mydomain.com/login/ or http://mydomain/login (which is actually http://mydomain.com/login.php) it redirects perfectly, but my href becomes as follows: mydomain.com/login/login.
As I understand the last slash is considered to be the root so the program appends the url to it, but i don’t want to. How can I avoid this phenomena, what is my problem?
Thanks for your help, in advance!
Hello,
I’ve read through and tried the .htaccess coding to no avail. I want to type in http://www.mydomain.com/greatpage and have it redirect to http://www.mydomain.com/greatpage.php
I have created the .htaccess file on my site and pasted in the coding listed above. Nothing. I use Godaddy, if that means anything. I also use Smultron to code my site.
Help me…
I want to change the my url
from: http://domain-name/dir/page1.php
to: http://domain-name/dir/?page1
For this i have written following code in my .htaccess file:
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+.php
RewriteRule (.).php$ /dir/?$1 [L,R=301]
RewriteRule (.)/$ $1.php [L]
The URL gets changed to what i want perfectly. But the page remains the same.
For example,
Suppose i m on page1.php. When i click on menu ‘page2’ (page2),
the url gets changed to “domainname/dir/?page2” but browser dont show the ‘page2’ contents.
I am not using any CMS. Its just a simple php website.
i was used the follwing code but not working for http://www.example.com/index.php to http://www.example.com/index
kindly help the correct code and how to used the code for our php file if any send me the mail with screenshot .
Thanks in advance..,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
Do you have to change the way you link to internal pages once you’ve implemented this? If so, how?
This one is my own creation;
AddType text/x-component .htc
RewriteEngine On
RewriteBase /
remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.).php\ HTTP
RewriteRule (.).php$ $1 [R=301]
remove index
RewriteRule (.*)/index$ $1/ [R=301]
remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1.php [L]
Pastie Link below, copy and paste from the blog may bork it up :)
http://pastie.org/6011480
Hello Niall…
I tried your code..
changed php to html in the code
however only ONE link is working from all links on the page.. others are showing “404” or access forbidden
I also added
Options -MultiViews as I am hosting with 1and1.com
below is exact code I have used in the .htaccess for your reference
Options -MultiViews
AddType text/x-component .htc
RewriteEngine On
RewriteBase /organic-herbal
RewriteCond %{THE_REQUEST} ^GET\ (.).html\ HTTP
RewriteRule (.).html$ $1 [R=301]
RewriteRule (.*)/index$ $1/ [R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1.html [L]
currently I have 4 pages for testing.. 1 is index.html
and other 3 pages.
I have tried numerous codes from various forums.. but not a single one is working..
if you can help I will be glad..
Prasadh
hey bro v nice its work thanks a lot but need some help about such type of url
http://www.asdf.com?pageid=34
want http://www.asdf.com/pageid/34
This works great for godaddy VPS thank man
FYI the above can be dropped into any Apache server and you can reference example.php or /example folders will also render correctly but dont put a file named foo.php and a folder called foo, Apache will get upset :)
Hope it helps :)
Perfect Niall, just perfect thank you :)
How would one tackle something a little more complex such as:
http://127.0.0.1/demo/redux/index.php?p=example.txt
Should work if the .htaccess is in the root no, other wise you would have to specify the domain, which I think is the earlier lines;
RewriteBase /
This should leave GET variable clean in the URL without the index.php
Niall Flynn’s code is the best one! It works just perfect on the http://www.equinelux.com
I’ve tested 25 different solutions, and only this one makes my morning happy. Thank you Naill. And now I wonder – is it really needed for SEO, or it is just …. ?
Yeah it is my baby, been working on it for a while, it does help with SEO, if anything I think it just makes sense not to show a file extension to a normal brochure audience.
I have a standalone post here now for this http://www.niallflynn.com/seo-news/remove-file-extension-from-urls/ so anyone who needs help or wants to add to this can drop some code/comments in.
Thanks, your post helped me am grateful.. thank you once more.. :D
Hi,
Brilliant post. Can anyone tell me how you can do it so it shows:
http://www.test.com/test.php as http://www.test.com/anythinghere/test.com
Thanks.
Jack
Might help;
http://stackoverflow.com/questions/704102/how-does-rewritebase-work-in-htaccess
Not 100% sure what you mean
Please try this :(works fine for me)
Make a .htaccess file on your web root and paste below code……..
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html
Note:Replace html with your file extension, eg:- php, htm, asp
this is the one that worked for me! Thanks!
This works for me but I was wondering, how could this be simplified..
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/$ $1.php
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)/$ $1.html
RewriteCond %{REQUEST_FILENAME}.py -f
RewriteRule ^(.*)/$ $1.py
Why? No real performance to speak of if it works, it works :)
Hi,
I have tried around 15 different codes to try and make the page load without .php at the end but none of them work.
I have contacted my host and they advised that their is nothing on the server which should stop a mod rewrite.
This is the latest one I have attempted. Do I need the file in a certain format. I downloaded the original blank htaccess file and opened it in Wordpad.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/$ $1.php
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)/$ $1.html
RewriteCond %{REQUEST_FILENAME}.py -f
RewriteRule ^(.*)/$ $1.py
I’m having issue getting RewriteRule to work on my site. I’ve used the following code, but it doesn’t seem to be working. I really don’t know how to get it to remove the .php when the page is loaded.
Thanks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html
Try to remove the file extension from the href code in the HTML after putting that code in the .htaccess.
Have you tried the fix here?
http://www.niallflynn.com/seo-news/remove-file-extension-from-urls/
I will try that now.
thanks
Hi Niall, I tried the code it works, but does the following
1) when i put my mouse on the link for example on “About us” and look down it is showing aboutus.php, but when i click it it will display http://www.mysite/aboutus with out the a forward slash on the front.
2) I don’t want people to see the .php when they place their cursor on the link before clicking
Please your help will be appreciated.
Thanks
Oh that is easy just remove the .php link fro m you a href;
So if you had;
My Link
Change it to;
My Link
This will fix it and then if someone tries to go to either /zyz.php or /zyz/ they will get the same thing :)
Ok will try that now.
Thanks
Hi Niall,
It worked fine.
sorry I’m asking too many question.
Also my site do somthing like http://www.mysite/news.php?id=1&content=23. How do I make it display http://www.mysite/news/1/23
Thanks
Chika
Hi
Can someone tell me how to do this trick but within a subdomain ?
eg:
http://www.mysite.com/sub/about.php
should be rewrite to :
http://www.mysite.com/sub/about
or
http://www.mysite.com/sub/about/
thanks
http://www.niallflynn.com/seo-news/remove-file-extension-from-urls/
As far as I remember it is the 3rd line RewriteBase / that needs to be changed.
I would checkout here http://www.silverstripe.org/general-questions/show/5168
I need to REMOVE part of the file name /extension.
From this:
http://example.com/news/local/nameoffile.REMOVE.shtml
to this:
http://example.com/news/local/nameoffile.shtml
Hi, I tried this code on my clients website but it doesn’t work. Simply I want to change the URL
http://www.ninacooke.co.uk/page.php?f=lorem
into
http://www.ninacooke.co.uk/page/lorem
my first step was to make http://www.ninacooke.co.uk/page work so I used this code:
RewriteEngine on
RewriteBase /
RewriteRule ^page$ page.php [L]
but it doesn’t work. Can somebody please tell me how should I do it?
ok I’ve changed the code a bit into:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^page/([a-zA-Z0-9]+)/$ page.php?f=$1
</IfModule>
but it still doesn’t work…
It should change
http://www.ninacooke.co.uk/page.php?f=lorem
into
http://www.ninacooke.co.uk/page/lorem
What do I do wrong?
I would use the following for testing this http://htaccess.madewithlove.be I can help you with this but best you get a nice test environment as I hate giving ppl 500 errors :)
I made it work moving http://www.ninacooke.co.uk/page.php?f=lorem into http://www.ninacooke.co.uk/page/index.php?f=lorem and using this code:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^page/([a-zA-Z0-9]+)$ page/index.php?f=$1
</IfModule>
now http://ninacooke.co.uk/page/lorem works ok :)
this is cool
Hey Monika, you code is not working.
Mine is localhost/d/page.php?id=contact and I have used your code..But its not working.
RewriteEngine on
RewriteBase /
RewriteRule ^d/([a-zA-Z0-9]+)$ d/index.php?f=$1
It should change to localhost/d/contact..But nothing happened????
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Worked fine for me, It looks like a lot of people are not realizing that in order for this to work, you need to navigate to the new URL without the extension ie . http://www.domain/help.php , you would browse to http://www.domain/help and the browser will load help.php…. it wont magically remove the .php extension if that is how you have typed in the URL or linked to the page with a href containing the .php extension.
Hope that helps..
Or you know, avoid the total overkill of
mod_rewrite
for such a task and simply useI have a website with wordpress blog. I have removed the php extension of all files in the root folder using htaccess. when i access http://www.mydomain.com/blog, it redirects to 404 page. I created a htaccess inside wordpress folder with Rewrite engine off but no use .
Kindly take this issue into serious concern. Thanks in advance.
I’m having the same issue. Is there a work around for this? Any sub-directories inside the root will be redirected to 404.
Anyone found a solution for this?
how remove .php in my static website.
please help.
I have a handy post here that might help http://www.niallflynn.com/seo-news/remove-file-extension-from-urls/
Thanks #Niall
Normally we use like this
after using .htaccess i get error for displaying image
if i give full url means it display fine like this
how to access my image without giving this url
Please give any suggestions
Hi! I am having a blogger and like other website’s cPanel, I am unable to find the .htaccess file which is in hidden shape. Please let me know that “How could I find the .htaccess file” in the root directory of my blogger, or I may allow, if created a new one .htaccess file for my blogger. Kindly solve my problem. Thanks and Best Regards for all the Readers.
I can’t get the trailing slash to work. When I use a version that has it, I get a 404. Please help.
Hello,
I am working on one website..I have a problem in .htaccess file when i hide url .php its work on localhost perfectly but when i upload on live site or server it may get error 404 file or directory not found…
I will change all the thing like http.conf mod enable and all that.. but no result found
my lots of time waste on this topic but no any output so plzzzz help me..
thank you in advance..
I am waiting for yours reply…
I don’t know about this. I would think removing your extensions would be problematic at best. 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. Maybe for a small site or blog, but for a large website it seems to carry too much risk. What about all of the links already pointing to your pages? 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 for higher traffic website, especially.
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. I question whether there is really any SEO value.
Hi,
I want to modified url in my site same as WordPress
I have: http://mysite.com/555005-remove-file-extention-from-urls.html
How to rewrite to:
http://mysite.com/remove-file-extention-from-urls
in htaccess?
Thanks.
Thanks Niall Flynn Your code worked perfectly.
It should work on nearly any Apache server, its an old one but seems to be still good.
Hi,
I want to modified url in my site (is built in classic asp)
I have: http://mysite.com/test.asp
How to rewrite to:
http://mysite.com/test
in htaccess?
Thanks.
IIS Servers dont use htaccess, it is an apache file for generally PHP settings unless I ma missing something. Search for IIS or ASPX re-write scripts should help :)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html
I have tried everything to no avail even putting html files in a sub directory and re-naming it index.html in a folder for eg:http://stuartmansfield.com/bio/index.html bio folder I am using dreamweaver cs6 fluidGrid layout html5 I think Go Daddy has no clue anyone know how? I tried all of the above i am at my whits-end.
In case any newcomers are having trouble with this. You have to remove all .html links from your source code for RewriteCond to work. This is easily done in Dreamweaver for anybody using it. Just use the ‘find’ function and type .html for your entire website and ‘replace all’ with nothing.
Tom, thanks that actually worked thanks!
Thanks a ton for this helpful tip!! Worked like a charm in my .htaccess file =D
I would like to redirect all pages WITHOUT an extension to pages with an .HTML extension.
Example – /my-page –to–> /my-page.html
Thank you!
Charlie
Hi every one
for remove .html
after try many different ways This worked for me . in .htaccess root directory :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $.html [L]
i am using ideahosting and how i can remove .html extension from HTML pages.
i want to access like this abc/about not a abc/about.html.
Can anyone help me ??
I used above code in .htaccess file but not working
Hosting Company is Go-Daddy but really can’t get this to work using Dreamweaver cc Bootstrap FTP the site for testing but still no go!
hi i am trying to make
ammaco.com/shop_home.php
toammasco.com/shop_home/
any help pleaseHello,
I am working on one website..I have a problem in .htaccess file when i hide url .php its work on perfectly.
Bu my issue is how to remove id this link
http://trustedbusiness.reviews/details.php?id=3
It’s urgent .
Can someone help me, how can i remove .html extension on my site? applies only for one page
how do i remove/hide the .html on my url? im using adobe muse and it’s hosted by web.com. please help, i tried all the codes listed here and none made any changes to the url.
I used a few Adobe CMS before never a huge fan, even at enterprise level, but thats just my opinion. Equally this and things like Kentico which I bundle in the ‘awkward to do normal stuff in’ bracket can normally be found in the CMS.
I can see some threads here also, searching for SEO friendly URLs might help;
https://forums.adobe.com/thread/1451607
I’ve used htaccess for other redirects, however, I’m confused about removing extensions and have a few questions.
If you manually remove the extension from a file name then you can’t view the page on your computer (I write code by hand so don’t use a CMS, etc). I tried setting up a site with MAMP (never again).
If you load the pages with .html extensions and remove the html with htaccess so it gets indexed without the .html I assume all outside links need the .html removed. Yes?
Also, I don’t see how this redirect hides anything as anyone can view the source code.
It’s a long time, but I want to make some points:
It’s, somehow, the matter of beauty (called “Pretty URLs” – which are more understandable); And not only for .html files! for all kind of files and most of them are not .html files, are -ugly- PHP/ASP files!
Also, you can do more advanced things like converting a query string to a plain URl.
Isn’t it nice to have:
“example.com/sample/anothersample1/anothersample2”
instead of long/ugly
“example.com/sample.php?longquery=anothersample1&anotherlongerquery=anothersample2”
(Off-course this is not mentioned here and needs some work and mind)
And it is not a redirect. It’s a rewright.
I was able to succeed using the .hatccess configuration:
Apache Rewrite Rules
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
DO NOT REMOVE THIS LINE AND THE LINES BELOW SSL_REDIRECT:infocare.tk
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^infocare.tk$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
DO NOT REMOVE THIS LINE AND THE LINES ABOVE SSL_REDIRECT:infocare.tk
Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.[a-zA-Z0-9]{1,5}|/|#(.))$
RewriteRule ^(.)$ $1/ [R=301,L]
Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)/$ $1.php
End of Apache Rewrite Rules
In my php file:
meu link
Works fine!
Thanks for help!
Hi…
Does this also work locally? I’m coding without using any local or online servers, but I keep getting this ‘file not found’ error.
I have /store.html and i want to use /store How can i achieve this? the snippet didn’t worked for me.