PHP URL Format without .php extension?
Solution 1
You need to use an .htaccess
file if you are using apache. If you are using IIS then you can configure it in IIS.
Have a look at this.
Your .htaccess
file would need to look something like this:
IndexIgnore * # prevent directory listing
Order deny,allow
Allow from *
# ------------------------------------------
# Rewrite so that php extentions are not shown
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
You cannot simply create a .htaccess
file in windows because of file constraints, so open a text editor (Notepad ++ or your programming IDE) and create a new file called .htaccess
. The .
is important in front of the file name.
Edit
Do not have duplicate file names. IE contact.php
and contact.js
as the htaccess
will not know which one to serve, which (depending on your apache) will either return an error page or it will serve one of them only.
As @AedixRhinedale pointed out in the comments below:
Just a note from Apache: You should avoid using .htaccess files completely if you have access to httpd main server config file. Using .htaccess files slows down your Apache http server. Any directive that you can include in a .htaccess file is better set in a Directory block, as it will have the same effect with better performance
Solution 2
It's as easy as this..
1) Create .htaccess file in directory (windows has restrictions around this as mentioned above).
2) Just Insert this..
# Turn mod_rewrite on
RewriteEngine On
##(Optional) Redirects http to https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#### hiding .php extensions below:
# (optional) Ignore logic.php .. any page that is needed as mvc with .php exstensions
RewriteCond %{THE_REQUEST} ^/logicpath/logic.php [NC]
RewriteCond %{THE_REQUEST} ^/logicpath/funcs.php [NC]
# (optional end ^)
## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
# (optional) Does the same for html paths
## To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo.html to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.php [L]
# (optional end ^)
As mentioned prior, the httpd main server config file is much better. I am not really sure how to work around the main config files, But I know it's not exatcly the same from small experience.
If someone else knows a good resource on learning how to modify config files please mention!
newCode
Updated on June 28, 2022Comments
-
newCode almost 2 years
all I need is to get a string as URL without showing the PHP filename. I have file contact.php page I am trying to get URL as e.g.
localhost/contact.php
this is what now I am getting I need to access this page just with
localhost/contact
For every page I should able to navigate by their name only.
localhost/contact
localhost/help
localhost/support -
al'ein almost 9 yearsAdding to your answer, a note from Apache: You should avoid using
.htaccess
files completely if you have access to httpd main server config file. Using.htaccess
files slows down your Apache http server. Any directive that you can include in a.htaccess
file is better set in aDirectory
block, as it will have the same effect with better performance. -
Jacques Koekemoer almost 9 years@AedixRhinedale thanks I added that to the answer and attributed the note to you :). I actually did not know that
-
newCode almost 9 yearsand what about when i am putting my website live! @JacquesKoekemoer
-
Jacques Koekemoer almost 9 yearsWell if you are using shared hosting the chances are great the you do not have access to the
httpd
file so copy the.htaccess
into your root of your code -
newCode almost 9 yearsGreat . Thank you :) @JacquesKoekemoer