0

I am a beginner and I have just hosted my first ever website; the problem I am faced with now is how to make my site visitors access pages on my site even without adding the page extension.

Example: www.example.com/services Instead of www.example.com/services.php

Wurie
  • 31
  • 3
  • See this answered already https://stackoverflow.com/questions/4026021/remove-php-extension-with-htaccess – user29671 May 04 '19 at 23:14
  • 4
    Possible duplicate of [Remove .php extension with .htaccess](https://stackoverflow.com/questions/4026021/remove-php-extension-with-htaccess) – Wai Ha Lee May 04 '19 at 23:24

3 Answers3

0

You can use url rewrite to append the extension when one is not specified. OR you could build a routing system to link specific uri to a specific resource (quick example here). The latter introduces a lot of complexity but gives much more control. For example, here is basic pseudo code based on a custom routing system I built:

$routes->addStaticRoute(
    /* Pattern */ '/home/myPage',
    /* Params  */ null,
    /* File    */ 'path/to/myPage.php'
);

All requests automatically go to index.php where my router translates the url request into an actual routes to a resource. With the above static route, if a user requests http://mySite/home/myPage, the router will respond with the static file at path wwwroot/path/to/myPage.php.

Another example of a static route:

$routes->addStaticRoute(
    /* Pattern */ '/home/{fileName}.{extension}',
    /* Params  */ ['fileName' => 'index', 'extension' => 'php'],
    /* File    */ 'path/to/{fileName}.{extension}'
);

If a user requests http://mySite/home the router will respond with the default wwwroot/path/to/index.php. Further, if they request http://mySite/home/page5, router will respond with (if exists) wwwroot/path/to/page5.php.

Routing is somewhat of an advanced concept and these are some oversimplified examples but I hope this gets you started.

Phillip Weber
  • 554
  • 3
  • 9
0

edit .htaccess file and add the following

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

and it will be done

Rawand
  • 16
  • 3
0

This is the usual solution:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule .* $0.php [L]

But if you want to use $_SERVER['PATH_INFO']) to allow:

http://my.domain/script/a/b
# instead of 
http://my.domain/script.php/a/b

... you need something like:

# specifix scripts:
RewriteRule ^(script1|script2)/(.*) /$1.php/$2 [L]
# or more general, but with issues for sub-directotries
RewriteRule ^([^./]+)/(.*) $1.php/$2 [L]

# final rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule .* $0.php [L]
Wiimm
  • 2,971
  • 1
  • 15
  • 25