Remove File Extention from URLs
RewriteRule ^about$ about.php [L]
That little bit will make http://example.com/about.php be able to load at http://example.com/about
RewriteRule ^about$ about.php [L]
That little bit will make http://example.com/about.php be able to load at http://example.com/about
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
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 onRewriteCond %{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 would like this snippet, but how can use this snippet? where i use it? anyone explain this deeply?
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.
@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?
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.
# 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
@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?
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, webmaster@example.com 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..
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
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.txtShould 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.
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 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