Hide .php without htaccess [duplicate] - php

How am I able to hide a .php extension in an URL address, so that this address:
http://www.thesite.com/somefile.php
would look like:
http://www.thesite.com/somefile
without the use of the .htaccess file. The reason for that being because I have many directories and would want to hide the extension on all those files in every directory. I have tried to set expose_php to off, and this still fails with error 404.
I am using PHP 5.3.10 and Apache server.

Although you specifically said no, using the .htaccess file would remove the .php extension from all PHP files in all subdirectories in a site. I think that is what you are going for.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>
Putting that into the .htaccess file will remove all the .php file extensions. Or you could put it directly into the webserver's configuration files.

You can achieve this with URL rewriting. If you don't want to use .htaccess, you can write the rule in your host configuration file.

Related

I am redesigning a PHP website in HTML. Do I need to set up redirects because of the file extension?

I am working on redesigning a website that is currently a PHP website. The new site will be all HTML. I am trying to keep all of the slugs the same. Should I set up redirects?
Currently, the website pages have the .PHP extension in the browser.
Example: https://www.dehartsystems.com/residential.php
The new page will be HTML and the URL will have no file extension.
Example: https://www.dehartsystems.com/residential
Do I need to set up redirects as the file extension will be changing?
To answer your question, yes you do need to set up redirects. Typically this is done in the .htaccess file in the root of your html folder on your server.
In this .htaccess file you put:
# Check rewriting is possible.
<IfModule mod_rewrite.c>
# Turn on Rewrite engine
RewriteEngine on
# check if file does not exist.
RewriteCond %{REQUEST_FILENAME} !-f
# check if folder does not exist.
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite the given URL.
RewriteRule ^(.*).php$ $1.html [R=301,L]
</IfModule>
This will redirect any call to any .php file that does not exist to the HTML file with the same file name, in the same folder location.
Reference:
https://htaccessbook.com/add-remove-change-file-extensions-htaccess/
See also How to change the PHP file extension using .htaccess file on GoDaddy Linux Hosting?
Yes, you will need to rename every file to .HTML if you don't do so the browser won't recognize the file as code. " https://www.dehartsystems.com/residential.html " only then your website will work., i.e you should rename residential.php to residential.html

custom php.ini and rewriterule in .htaccess

I have a php application, which requires an .htaccess file with this rewriterule in it:
RewriteRule .* blog_manager/controller/frontController.php
I've opened a new hosting account and I wanted them to disable the extension:
suhosin.mt_srand.ignore
for that they added the following lines to the .htaccess file, which are supposed to load a custom .ini file that disables that extension:
AddHandler phpini-cgi .php
Action phpini-cgi /cgi-bin/php5-custom-ini.cgi
but then, I started getting 500 Internal server error...
apparently, the rewriterule and the lines they added don't work together.
I've tried to disable that extension with ini_set() in the code and with php_flag/php_value in the htaccess file, but both failed.
does anybody know why is that? Is there anything that can be done to allow the rewriterule and still disable that extension?
Change your rule to this:
RewriteCond %{REQUEST_URI} !/blog_manager/controller/frontController.php [NC]
RewriteRule ^ blog_manager/controller/frontController.php [L]

PHP files without extensions

Is it bad to use:
ForceType application/x-httpd-php
and to save files without a file extention (e.g. index instead of index.php)? The intention is to hide/remove .php from the URL and to stop users from manually putting e.g. /example.php.
To remove the file extension, add this to the .htaccess :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
change .php to the proper file extension
new link :
link text
Edit :
Save your files as index.php, about.php, and so on
Yes. Yes it is bad.
The right way to do that is by using mod_rewrite and .htaccess files.
Its not good practice to change extension as it will need configuration for web server each time and so its a portability issue.
You should use .htaccess directives to setup any level of customization.
And in best practice you can route all requests to index.php to avoid direct access of php files.

For simple web application, how can you use ".main" as the URI extention in place of .php or .html ?

For simple web application, how can we use ".main" as the URI extention in place of .php or .html ? How can we change example.com/test.php to exmple.com/test.main, without actually renaming the file.
Thanks
You can use the apache extension mod_rewrite. Here's a sample of what you can do.
RewriteEngine On
RewriteRule ^(.*)\.main$ $1.php
This will take any request with the extension .main and actually serve the file with the same name but extension .php.
ModRewrite is a great solution for such things, however, to permanently have a different extension for you PHP files while they are still recognised and executed as PHP, use Apache's SetHandler module.
Options +Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.main$ $1.php [R=301,L]
Also please see this Tutorial: An In Depth Guide to mod_rewrite for Apache http://net.tutsplus.com/tutorials/other/a-deeper-look-at-mod_rewrite-for-apache/
In IIS, you would use the Manage Handlers script to assign the PHP FastCGI executable to .main file extensions.

How to hide PHP file extension without using .htaccess

How am I able to hide a .php extension in an URL address, so that this address:
http://www.thesite.com/somefile.php
would look like:
http://www.thesite.com/somefile
without the use of the .htaccess file. The reason for that being because I have many directories and would want to hide the extension on all those files in every directory. I have tried to set expose_php to off, and this still fails with error 404.
I am using PHP 5.3.10 and Apache server.
Although you specifically said no, using the .htaccess file would remove the .php extension from all PHP files in all subdirectories in a site. I think that is what you are going for.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>
Putting that into the .htaccess file will remove all the .php file extensions. Or you could put it directly into the webserver's configuration files.
You can achieve this with URL rewriting. If you don't want to use .htaccess, you can write the rule in your host configuration file.

Categories