/    Sign up×
Community /Pin to ProfileBookmark

How do I remove file extensions from my URl

Hi everyone,

So I’ve been wondering how to hide extension from my URl. Say I have [url]www.mysite.com/about/about-us.html[/url] or [url]www.mysite.com/contact.php[/url] and I want it to show as [url]www.mysite.com/about/about-us[/url] or [url]www.mysite.com/contact[/url] how do i do that?

needless to say I’ve tried out severally php codes on several forums and none of them really works. So am wondering if there’s anybody that can show or offer me a really good working code.

Thanks in advance

to post a comment
Full-stack Developer

2 Comments(s)

Copy linkTweet thisAlerts:
@Kevin2Dec 08.2013 — For small sites, 5-10 pages, the easiest thing to do is give each file its own folder and name it index.yyy. For example, www.mysite/about/about-us/index.html. Now your link code can look like <a href="/about/about-us">about us</a>. Apache will serve index.html (or .php, or .htm) by default, but will also serve it with a trailing slash on the URI that shows in the browser. So what the user will see will be www.mysite/about/about-us/ in the address bar.

Somewhat obviously, once you get beyond just a few files this becomes impractical.

There may be a .htaccess solution, but I'm a pretty poor regex coder. You might look it up though.
Copy linkTweet thisAlerts:
@stiniauthorDec 09.2013 — Ok. Was able to get a working code. Thanks Kevin2

Here's the simple code I used, just in case someone else wanna know:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

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

If you want to remove the .html extension from a html file then you simply have to alter the last line from the code above to match the filename:

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

Success!

Help @stini spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...