R
R
rinaz222020-02-09 09:17:10
htaccess
rinaz22, 2020-02-09 09:17:10

How to hide page format in url but not in php?

Hello! I want to hide the page format, for example, instead of /index.php it should turn out to be /index.
To do this, I used to do without htaccess, using php, I wrote a script that checks if there is a file with the php format in the necessary directives. Those. takes a url (for example index) and checks:

$page = "index";
if (file_exists("page/$page.php"))
  include "page/$page.php";

Works well, but there was a problem with the fonts. When I specify the path to the font, /fonts/font1.ttf the font is not found. As far as I understand, this is because of my script, i.e. it inserts .php at the end of the url. Although the pictures open (img/img1.png), I didn't specify anything in the script for the pictures.
Therefore, I began to look for another way to hide the page format.
How to hide format in url and when using $_SERVER['REQUEST_URI'] it showed with format(index.php)?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
rinaz22, 2020-02-09
@rinaz22

Solution:
I specified the following in htaccess:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php

Now the request is made only if the file does not exist at the specified path. And in order not to accidentally stumble upon the direct link of the page (page/index.php) indicated a 404 error:
RedirectMatch 404 ^/page/.*

G
granty, 2020-02-09
@granty

rinaz22 , the solution is correct, only at the same time you need to correctly make index.php, since "everything not found" is wrapped in it, including pictures, scripts, styles, etc.
Your index.php should quickly recognize and "kick back" all invalid urls. If someone puts < img src='your_site/non-existent_image.jpg'>
anywhere on the visited page , then your index.php will constantly twitch (the same will happen with your non-existent images). If index.php includes a bunch of other modules, and runs long enough, your site will crash.
And keep in mind that Googlebots regularly request urls like "any_abracadabra.html", expecting to get 404 Not found on them. Your index.php will intercept such urls, it's not bad that it gives the 404 code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question