mod_rewrite does not working while trying to hide .php extension - php

how properly can I hide .php extensions form my url : example:
www.example.com/index.php
to
www.example.com/index
My .htaccess file content is:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
And it is not working although phpinfo() shows that mod_rewrite is in "Loaded Modules" section. What's wrong here?
When I tried to set Authentication:
AuthUserFile /usr/home/vesa/.htpasswd
AuthGroupFile /dev/null
AuthName Somewhere Neat
AuthType Basic
<Limit GET POST>
require user vesa
</Limit>
enter code here
It asked for username and password. Therefore I assume that .htaccess files are processed by server. Right?
When I try to add
Options +FollowSymlinks
I get 500 Internal Server Error
When I add RewriteRule (.*)\.asp $1.php it successfully changes any .asp extension to .php
When I use RewriteRule it just redirects my browser to another file. So in case if I will try to hide extension it should redirect me to file with no extension - in which case I will get error because there is no such file exists. What is the simplest way to make mod_rewrite "hide" (write url filenames without extension .php) while linking to .php files?

Is your PHP file sending a redirect to force the user to index.php? Use something like firebug to see what's going on in the headers.

Probably, there is some configuration set that allows (for .htaccess file) to redirect to any file, it is basically rewrite URL, but in the same time it is not allowing Options +FollowSymlinks. I'm not familiar with php config file, but i am sure system administrators knows what is going on.

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

Hide .php without htaccess [duplicate]

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.

Can't redirect with .htaccess

On the end of my rope here, nothing that should be working is.
I'm new to website building and I noticed my visitors were trying to access certain pages and getting forbidden warnings e.g.: trying www.test.com/test you give a 403 error.
All I want to do is make it so www.test.com/test would redirect it to a test.html file in a directory right above it.
Tried doing it with .htaccess in the root directory with nothing but
redirect /test www.test.com/test.html
Doesn't work. Just gives me the 403 error.
Tried a simple index.html file to redirect with nothing but
<?php
header('Location: www.test.com/test.html');
die();
?>
But that doesn't work either.
Really not sure why both of these outright refuse to work.
header parameter should be wrapped in double quotes. like
header("Location: www.test.com/test.html");
exit();
also make sure your server recognizes your file extention as a php file if its not having a .php file extension. You can add the custom extensions by editing httpd.conf.
If I am interpreting correctly your ultimate intention, you don't need to use redirections, just try this in your .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.html [QSA,L]
</IfModule>
This way, every requested URL will be interpreted as if it was the same request plus .html.
Although you could do it via a redirection, it is not necessary.
So once you have that code in your .htaccess you just need to have the file test.html in your root, and www.test.com/test will serve the www.test.com/test.html page.

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.

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