PHP URL Format without .php extension?
Asked Answered
T

3

11

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

Tripp answered 21/8, 2015 at 12:8 Comment(1)
mod_rewrite and .htaccess is your friendBuckskins
E
24

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

Ewe answered 21/8, 2015 at 12:18 Comment(5)
Adding 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 a Directory block, as it will have the same effect with better performance.Greywacke
@AedixRhinedale thanks I added that to the answer and attributed the note to you :). I actually did not know thatEwe
and what about when i am putting my website live! @JacquesKoekemoerTripp
Well 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 codeEwe
Great . Thank you :) @JacquesKoekemoerTripp
S
1

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!

Studnia answered 14/6, 2019 at 17:22 Comment(0)
K
1

Just create a directory in root folder (wwwroot or www etc)

Name it as 'contact'

Put your php file in this folder Name it as 'index.php'

Use links as https://example.com/contact or https://example.com/contact/?c=somehing

Kathrynkathryne answered 12/3 at 8:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.