1,105,226 Community Members

remove ".php" from URLs wth .htaccess file

Member Avatar
jonow
Junior Poster
104 posts since Jan 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Is it possible to remove the suffix of URLs using the .htaccess file?

For example you have http://www.example.com/page.php can you change it to http://www.example.com/page without the .php. I want it to work both ways, with the .php and without.

Is there a way to do it with the .htaccess file? if so how?

If you do it a different way how do you do it?

Member Avatar
dnns
Newbie Poster
2 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi,
Yes, you can do it through .htaccess file... it called "mod_rewrite", just google it (.htaccess mod_rewrite) and you will find tens of guides, short and long.

Good luck!

Member Avatar
jonow
Junior Poster
104 posts since Jan 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks for your help. I thought it was mod_rewrite but wasn't sure. Can you also use this to get rid of the question mark (?) made by PHP? Like if you have a pagination and it has this after the page : "?page=1" caa you get it to change it to "-page1" or can you not do that through the .htaccess file. Or can you do it through the PHP or can you not change that at all.

Member Avatar
OmniX
Practically a Master Poster
652 posts since Dec 2007
Reputation Points: 21 [?]
Q&As Helped to Solve: 10 [?]
Skill Endorsements: 0 [?]
 
0
 

Additional questions,
What if you used mod_rewrite to remove the whole filename?
If you did this what would have to $_GET variables?

Just wondering, Thanks!

Member Avatar
dnns
Newbie Poster
2 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

You can remove the question mark, for example, you can do the next change:
from: www.example.com/forum.php?topic=11&r=22
to: www.example.com/forum/11/22
or: www.example.com/t11/r22
or as you wish... you just define it in the .htaccess file.

It still using the $_GET function, what means you still able to use regular links (with full file names and question marks...) and you able to get variables through the GET function.
You can define in .htaccess file to change "topics" to "bananas", but the GET will get it as "topics".

Overall, I'm not a professional programmer, but I checked these options and it works for me 100%. After understanding how to work with mod_rewrite, it is very simple and short to do such changes.

Member Avatar
hemgoyal_1990
Junior Poster
176 posts since Aug 2007
Reputation Points: 5 [?]
Q&As Helped to Solve: 17 [?]
Skill Endorsements: 0 [?]
 
0
 

To Hide .php Extension from your all files then Use the Copy Below Code and Paste it in .htaccess file:

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

The above code is Tested and Work Fine with all Your Server Files.
Example: if your filename is "www.yoursite.com/file.php" then your may access this file directly with "www.yoursite.com/file" or "www.yoursite.com/file.php"

Member Avatar
OmniX
Practically a Master Poster
652 posts since Dec 2007
Reputation Points: 21 [?]
Q&As Helped to Solve: 10 [?]
Skill Endorsements: 0 [?]
 
0
 

hemgoyal_1990 can you explain your code in more detail?
example what the -d and -f do?
also in the url what is shown? just /file or /file.php? possibly to remove it completly or just the .php?
if so thats how to remove the file totally and access the get variables as well, like previously stated?
Trying to learn mod_rewrite it a big big topic.

Member Avatar
hemgoyal_1990
Junior Poster
176 posts since Aug 2007
Reputation Points: 5 [?]
Q&As Helped to Solve: 17 [?]
Skill Endorsements: 0 [?]
 
1
 

hemgoyal_1990 can you explain your code in more detail?
example what the -d and -f do?
also in the url what is shown? just /file or /file.php? possibly to remove it completly or just the .php?
if so thats how to remove the file totally and access the get variables as well, like previously stated?
Trying to learn mod_rewrite it a big big topic.

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

in the above code the -d define for Directory and -f for Regular File.
and the above will remove .php extension from your URL.
for ex: if ur page name is test.php in main directory then you may access your page with the following urls:
http://domain/test
or
http://domain/test.php
the above is work in both condition.

Member Avatar
moerpheus
Junior Poster in Training
86 posts since Jun 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

Nice thread! very helpful!
:)

Member Avatar
hemgoyal_1990
Junior Poster
176 posts since Aug 2007
Reputation Points: 5 [?]
Q&As Helped to Solve: 17 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanx alimoe for your comment.

Member Avatar
moerpheus
Junior Poster in Training
86 posts since Jun 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

you welcome hemgoyal_1990...

Member Avatar
OmniX
Practically a Master Poster
652 posts since Dec 2007
Reputation Points: 21 [?]
Q&As Helped to Solve: 10 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks hemgoyal_1990, ill get to that asap.
Also I would like to change www.domain.com/domain1/domain2.php to www.domain.com/domain2.php. So i would just use the -d in conjuction with the orginal code so I would get www.domain.com/domain2 ?
Thanks for your help, Regards X

Member Avatar
OmniX
Practically a Master Poster
652 posts since Dec 2007
Reputation Points: 21 [?]
Q&As Helped to Solve: 10 [?]
Skill Endorsements: 0 [?]
 
0
 

Also when I use your code my server spits out the error "Internal Server Error" any ideas why? I gather something to do with the php.ini file?

susanta-roy
Newbie Poster
1 post since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

How is it possible to remove php script file from url. like www.mysite.com/<SCRIPT_FILE>/product

Joy Kumar

Member Avatar
cmps
Posting Whiz in Training
226 posts since Nov 2011
Reputation Points: 26 [?]
Q&As Helped to Solve: 32 [?]
Skill Endorsements: 3 [?]
 
0
 

Dear "jonow", why don't you just use the MVC frameworks ?
All what you are talking about are included in those frameworks ...
Wordpress (Not the best example) for example uses the MVC structure, and if you notice the link looks like this domain.com/class/function/value

You can use .htaccess to modify the URL, but presonally I don't recommend you to use it, since I think that you will face some problems in the future when trying to modify some code or develop more pages.

Good Luck

Member Avatar
diafol
Where are my eyes?
12,939 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 90 [?]
Moderator
Featured
Sponsor
 
0
 

@amirbeb
I think it's safe to say jonow has either found his solution or has gone elsewhere. 2/3 years since his post and a while since he peeped in here.
This thread was resurrected by susanta-roy.

How is it possible to remove php script file from url. like www.mysite.com/<SCRIPT_FILE>/product

If your have just the one front page like index.php and everything else is bummed off that as amirbwb states, you can certainly do without it. However, if you have different pages such as:

www.mysite.com/computers.php?prod_id=1 TO www.mysite.com/1/
www.mysite.com/printers.php?prod_id=7 TO www.mysite.com/7/
www.mysite.com/keyboards.php?prod_id=34 TO www.mysite.com/34/

It may not be as straightforward. SO for example, if you had: www.mysite.com/1/ how would the system know which page to serve - computers.php, printers.php or keyboards.php?

Member Avatar
amill
Newbie Poster
1 post since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i currently have a well placed site on google which was built using html. all the referenced pages end in .php

i am redesigning the site using wordpress and i would like to put a .htaccess modification so that people who click on the existing links from external sites land on the new pages that dont have .php at the end.

Example

Existingpage.com/clients.php ---> this page is referenced

new style

Existingpage.com/clients/ ---> this is the new address which was created with wordpress.

Any help would be appreciated.

Member Avatar
ShoppingMonk
Light Poster
29 posts since Nov 2012
Reputation Points: -6 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
-2
 

thanks for sharing and my question is about why should we need to delete the files..?can any one help
Thanks for quality post

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article