Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP URL Format without .php extension?

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

like image 521
newCode Avatar asked Aug 21 '15 12:08

newCode


People also ask

What are PHP file extensions?

php file extension refers to the name of a file with a PHP script or source code that has a ". PHP" extension at the end of it. It's similar to a Word file with a . doc file extension.


2 Answers

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

like image 96
Jacques Koekemoer Avatar answered Sep 28 '22 15:09

Jacques Koekemoer


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!

like image 44
Charles Derek Avatar answered Sep 28 '22 15:09

Charles Derek