1.11M Members

remove ".php" from URLs wth .htaccess file

 
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?

 
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!

 
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.

 
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!

 
-1
 

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.

 
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"

 
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.

 
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.

 
0
 

Nice thread! very helpful!
:)

 
0
 

Thanx alimoe for your comment.

 
0
 

you welcome hemgoyal_1990...

 
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?

 
0
 

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

Joy Kumar

 
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

 
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?

 
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.

 
-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

 
0
 

the code is not working with my site
www.renet.co.in

You
Post:
Start New Discussion
View similar articles that have also been tagged: