0

Alright, so the only way I know of changing links to not show the file extensions;

this yourwebsite.com/customlink.php to yourwebsite.com/customlink

is either creating a folder called customlink and shoving an index file in there. Or creating a 404 page which snoops around the URL and grabs whatever string is behind the last / and does whatever to show the proper content.

My question is if this way of solving the problem is straight out idiotic, or not? I'm doing this because in my mind it saves space, let me explain: Every page that needs a customlink are the same with some bits and content taken from other places, so instead of creating X amount of folders and index files that includes a main file, I'll just have one 404 file that can handle it.

I apologize in advance if this is really stupid

skrilled
  • 5,350
  • 2
  • 26
  • 48
user1768788
  • 1,265
  • 1
  • 10
  • 29
  • 1
    I don't think this is tagged right at all. – Jay Blanchard Mar 18 '16 at 18:03
  • The proper way to do this would be either to let MultiViews do their job (can have side effects), or use mod_rewrite. Or, in Apache 2.4, [`FallbackResource`](http://httpd.apache.org/docs/2.4/en/mod/mod_dir.html#fallbackresource). Mis-using the 404 error document for this is likely to flood your error log with 404 messages. – CBroe Mar 18 '16 at 18:04
  • 1
    Possible duplicate of [Reference: mod\_rewrite, URL rewriting and "pretty links" explained](http://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained) – chris85 Mar 18 '16 at 18:06

2 Answers2

2

As you are not mentioning what server your pages are running on (Apache, IIS, ...?), I'll just assume Apache.

Put an .htaccess file into the root of your site with the following content:

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

It will internally rewrite everything without an extension to the corresponding php file, provided that:

  • the request is not a valid directory
  • a file with a php extension is in fact present
Constantin Groß
  • 10,719
  • 4
  • 24
  • 50
0

A more common approach is to route all HTTP requests to a single index.php using mod_rewrite in a .htaccess.

Then, based on the requested resource, the index.php outputs the appropriate file, whether it is generated from a database or nested in some other folder.

It's probably not a good idea to use a 404 page (an error page) for anything other than "File Not Found" instances.

jeffjenx
  • 17,041
  • 6
  • 57
  • 99