I want to hide extensions on my webpage using the .htaccess file.

I have managed to do it by using this code;

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

So when I visit mysite.com/index it shows my index.php. That's fine but I want to block visitors from visiting mysite.com/index.php so it should be redirected to mysite.com/index

I think I have found a way to do that. However, I will be using querystrings on my webpage. There will be some pages which I will need to carry the variables via GET method.

Normally a link with an id variable is like this; index.php?id=xx

However, if I redirect /index.php to /index my variables will be gone. So I want the urls to seem like this when carrying a variable mysite.com/index?id=xx

The extension will be PHP always. But I want this system work in all pages with all variables. I am not sure how many pages and variables there will be so do I have to write a rule in htacces for each of them or can I define a general rule? So it will be like this

index.php?id=xx will be index?id=xx
index.php?page=xx will be index?page=xx
users.php?uid=xx will be users?uid=xx
news.php?nid=xx will be news?nid=xx

This could probably be achieved using .htaccess file but I don't know, how can I do that.

P.S. Although I am pretty good at PHP, I don't know anything about creating a rule for htaccess file I just use the codes I find on the internet.

| |
  • Your duplicate post has been migrated here from StackOverflow. I merged the answers on this with it. – Stephen Ostermiller Dec 22 '14 at 11:44