.htaccess php extension removal not working - php

I am currently using an htaccess file to remove the php extension from my website files. It is working properly on one of my websites, but not on another and we're using the same code. (but with additional redirects on the one that is broken)
The weird part is - it has been working for months and only stopped working when Media Temple updated our grid server from Debian Squeeze to Wheezy (and they claim that shouldn't have changed how the htaccess works, but not sure what other explanation there is.)
Apparently it does still redirect html files. Any ideas why?
Current code:
# BEGIN (mt) controlled settings
<IfModule !mod_fcgid.c>
AddHandler php-stable .php
</IfModule>
<IfModule mod_fcgid.c>
AddHandler fcgid-script .php
<Files *.php>
Options +ExecCGI
</Files>
</IfModule>
# END (mt) controlled settings
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
ErrorDocument 403 /error-403
ErrorDocument 404 /error-404
ErrorDocument 500 /error-500
That code is followed by our 301 Redirects.

The host finally figured out the issue after saying it wasn't the update to Debian's fault. This is what they said:
the recent Wheezy update to the Grid had indeed modified a few >unexpected things, including the way MIMEs are configured which is why >+MultiViews was working for [site 1], which contains only .html files, >while [site 2] was not working, which contains .php files. To correct this, I have added the following line to the ~/domains/[site 2]/.htaccess file so that it interprets .php files correctly:
AddType application/x-httpd-php .php
So the code now looks like this:
AddType application/x-httpd-php .php
# BEGIN (mt) controlled settings
<IfModule !mod_fcgid.c>
AddHandler php-stable .php
</IfModule>
<IfModule mod_fcgid.c>
AddHandler fcgid-script .php
<Files *.php>
Options +ExecCGI
</Files>
</IfModule>
# END (mt) controlled settings
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]

Related

html page not reading php - ignoring .htaccess instructions

After upgrading my php version from 5.6 to 7.4, and revising my .htaccess file to acknowledge the new version, my html page is not processing its php code. In other words, the php code itself is output instead of its intended calculations. All php code worked fine before the version upgrade.
In the revised .htaccess file, version 7 replaced version 5 in the AddHandlers. Using phpinfo I checked to see if the php version was in fact correct. It was correct.
I know the code on my page is correct because it runs as it should when that page uses a .php extension in place of the .html extension. It worked fine prior to the version upgrade. What is wrong with my revised .htaccess? First, here is my revised .htaccess file:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
RewriteOptions inherit
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php74” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php74 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
RewriteEngine on
<Files index.html>
AddHandler application/x-httpd-php7 .html
</Files>
<Files index2.html>
AddHandler application/x-httpd-php7 .html
</Files>
<Files index-threejs.html>
AddHandler application/x-httpd-php7 .html
</Files>
...and here is my old .htaccess file that worked fine before the php upgrade:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#RewriteEngine On
#RewriteCond %{HTTP_HOST} dentonrainfall\.com [NC]
#RewriteCond %{SERVER_PORT} 80
#RewriteRule ^(.*)$ https://dentonrainfall.com/$1 [R,L]
RewriteEngine on
<Files index.html>
AddHandler application/x-httpd-php5 .html
</Files>
<Files index2.html>
AddHandler application/x-httpd-php5 .html
</Files>
<Files index-threejs.html>
AddHandler application/x-httpd-php5 .html
</Files>
RewriteEngine Off
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://dentonrainfall.com/$1 [R,L]
Jan's suggestion was the solution. To repeat what they said:
I noticed your custom AddHandler statements use a different MIME type from the one apparently auto-generated by cPanel, have you tried using application/x-httpd-ea-php74

Codeigniter Default Controller 403 Directory access is forbidden

Im using php 5.6.40 and codeigniter 3.1.9 on Mac OS Catalina
myroutes :
$route['default_controller'] = "Homepenta";
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteEngine On
<Files .htaccess>
order allow,deny
deny from all
</Files>
<IfModule mod_rewrite.c>
RewriteEngine on
Options All -Indexes
</IfModule>
# php -- BEGIN cPanel-generated handler, do not edit
# This domain inherits the “PHP” package.
# php -- END cPanel-generated handler, do not edit
on browser
http:\\localhost\mastertransaksi\
show error
Directory Access is Forbidden
but if
http:\\localhost\mastertransaksi\Homepenta
its work
anyone can explation how to fix this? Thanks
It looks like your DirectoryIndex is not set, or not set correctly (it defaults to index.html only). Add the following to the top of your .htaccess file:
DirectoryIndex index.php
Your mod_rewrite directives are not rewriting /mastertransaksi/ to /mastertransaksi/index.php because this is a physical directory and your rule excludes directories.
The 403 results because directory indexes are disabled and no DirectoryIndex document is found.
Aside: You have multiple RewriteEngine directives which are unnecessary and should be removed. Also...
<IfModule mod_rewrite.c>
RewriteEngine on
Options All -Indexes
</IfModule>
Remove the <IfModule> wrapper and RewriteEngine On directive. Just keep the Options directive.

How can you fetch a name from a Directory like you do with $_GET in PHP? [duplicate]

I want a mod_rewrite rule set, so I can refer to a page without the .php extension, but have that rewritten to include the .php extension. This will be running on a 1&1 server.
Are there any good references so I can learn more myself?
RewriteEngine On
RewriteRule something something.php [L]
http://example.com/something will be handled as if it was a request for something.php
To redirect all requests that are not a physical file to the same name but with .php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
The accepted answer above does not remove the .php extension from urls it just allows you to visit .php files without extension. To remove the .php extension completely from urls , you can use the following Rules in root/.htaccess :
RewriteEngine on
#1)externally redirect "/file.php" to "/file"
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
#2)Internally map "/file" back to "/file.php"
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,L]
Probably what you really want is just a way to not have the suffix at all. The
best way I've found of doing this is to use the Files directive in .htaccess or in
apache configuration files:
<Files myscript>
SetHandler cgi-script
</Files>
This avoids some of the downsides of rewrites, e.g., direct access of the .php
file.
.htaccess file: add the following lines:
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
https://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/
Now you would have to check for the AllowOverride condition in apache2.log file:
set options AllowOverRide All in your web root directory:
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
If you try to access your webpage without the .php extension, you may run into the following error in your apache error.log file:
/var/www/html/web/.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration
To fix this: You don't have the mod_rewrite module installed, To install it:
Run the following command: ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load
.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration
Again, after refreshing the webpage, you might get the folloowing error in error.log file:
[negotiation:error] [pid 4650] [client 127.0.0.1:46461] AH00687: Negotiation: discovered file(s) matching request: /var/www/html/web/test (None could be negotiated). (I was trying to access localhost/web/test.php using the url: localhost/web/test)
To fix it you would have to add the following lines in apache2.conf:
<IfModule mod_mime.c>
AddType application/x-httpd-php .php
AddType application/x-httpd-php .phtml
AddType application/x-httpd-php .php3
AddType application/x-httpd-php .php4
AddType application/x-httpd-php .html
AddType application/x-httpd-php-source .phps
</IfModule>
https://forums.digitalpoint.com/threads/htaccess-addtype-application-x-httpd-php-php-htm-html-and-maybe-a-fix.7584/#post-2491687
Now, I can access the test.php file using just the filename: test
For some reason I wasn't able to get any other solution working properly. I put this in an .htaccess file at the root of my site. Hopefully this will work for you.
RewriteEngine on #only use this once per .htaccess
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
It's important to keep in mind that if your header or URL contains the .php extension it won't be removed. You'll need to remove the extension.
Use this...
header 'location: /login';
rather than this...
header 'location: /login.php';
This Option will make clean SEO friendly URL's. Removing.php extensions from the visible URL, and ensuring that URL's accessed without the .php will still display the .php file. I was unable to find another answer that achieved this on multiple threads.
Remember to enable mod rewrite. On Debian or Unbuntu linux the following will work
sudo a2enmod rewrite
and
sudo service apache2 restart
Modify the corresponding section of your apache2.conf file to the following.
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
</Directory>
If using .htaccess to re-write ensure that the apache2.conf at least has these options enabled.
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
RewriteEngine On
</Directory>
Finally on some Apache install you may need to also set
AllowOveride All
in a .conf file in /etc/apache2/sites-available or /etc/apache2/sites-enabled.
Rewriting from apache2.conf file as opposed to .htaccess is rumored to be faster.
To get http://example.com/test linking http://example.com/test.php use:
RewriteEngine on
RewriteRule ^([^\.]+)$ /folder/$1.php [NC,L]
For directing a few single files it might be easier to avoid the RewriteEngine.
I've used this:
<Files $filename>
SetHandler application/x-httpd-php
</Files>
That's less powerful and flexible than rewrite but probably faster and seems like the way to go if you wish to just have a few specific files handled as PHP.
RewriteEngine On
# Redirect sites with no extension to .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule .* %{REQUEST_URI}.php
This is what I came up with. This is based off of the accepted answer and its comments.

Removing .php also messes up folders [duplicate]

I want a mod_rewrite rule set, so I can refer to a page without the .php extension, but have that rewritten to include the .php extension. This will be running on a 1&1 server.
Are there any good references so I can learn more myself?
RewriteEngine On
RewriteRule something something.php [L]
http://example.com/something will be handled as if it was a request for something.php
To redirect all requests that are not a physical file to the same name but with .php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
The accepted answer above does not remove the .php extension from urls it just allows you to visit .php files without extension. To remove the .php extension completely from urls , you can use the following Rules in root/.htaccess :
RewriteEngine on
#1)externally redirect "/file.php" to "/file"
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
#2)Internally map "/file" back to "/file.php"
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,L]
Probably what you really want is just a way to not have the suffix at all. The
best way I've found of doing this is to use the Files directive in .htaccess or in
apache configuration files:
<Files myscript>
SetHandler cgi-script
</Files>
This avoids some of the downsides of rewrites, e.g., direct access of the .php
file.
.htaccess file: add the following lines:
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
https://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/
Now you would have to check for the AllowOverride condition in apache2.log file:
set options AllowOverRide All in your web root directory:
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
If you try to access your webpage without the .php extension, you may run into the following error in your apache error.log file:
/var/www/html/web/.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration
To fix this: You don't have the mod_rewrite module installed, To install it:
Run the following command: ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load
.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration
Again, after refreshing the webpage, you might get the folloowing error in error.log file:
[negotiation:error] [pid 4650] [client 127.0.0.1:46461] AH00687: Negotiation: discovered file(s) matching request: /var/www/html/web/test (None could be negotiated). (I was trying to access localhost/web/test.php using the url: localhost/web/test)
To fix it you would have to add the following lines in apache2.conf:
<IfModule mod_mime.c>
AddType application/x-httpd-php .php
AddType application/x-httpd-php .phtml
AddType application/x-httpd-php .php3
AddType application/x-httpd-php .php4
AddType application/x-httpd-php .html
AddType application/x-httpd-php-source .phps
</IfModule>
https://forums.digitalpoint.com/threads/htaccess-addtype-application-x-httpd-php-php-htm-html-and-maybe-a-fix.7584/#post-2491687
Now, I can access the test.php file using just the filename: test
For some reason I wasn't able to get any other solution working properly. I put this in an .htaccess file at the root of my site. Hopefully this will work for you.
RewriteEngine on #only use this once per .htaccess
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
It's important to keep in mind that if your header or URL contains the .php extension it won't be removed. You'll need to remove the extension.
Use this...
header 'location: /login';
rather than this...
header 'location: /login.php';
This Option will make clean SEO friendly URL's. Removing.php extensions from the visible URL, and ensuring that URL's accessed without the .php will still display the .php file. I was unable to find another answer that achieved this on multiple threads.
Remember to enable mod rewrite. On Debian or Unbuntu linux the following will work
sudo a2enmod rewrite
and
sudo service apache2 restart
Modify the corresponding section of your apache2.conf file to the following.
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
</Directory>
If using .htaccess to re-write ensure that the apache2.conf at least has these options enabled.
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
RewriteEngine On
</Directory>
Finally on some Apache install you may need to also set
AllowOveride All
in a .conf file in /etc/apache2/sites-available or /etc/apache2/sites-enabled.
Rewriting from apache2.conf file as opposed to .htaccess is rumored to be faster.
To get http://example.com/test linking http://example.com/test.php use:
RewriteEngine on
RewriteRule ^([^\.]+)$ /folder/$1.php [NC,L]
For directing a few single files it might be easier to avoid the RewriteEngine.
I've used this:
<Files $filename>
SetHandler application/x-httpd-php
</Files>
That's less powerful and flexible than rewrite but probably faster and seems like the way to go if you wish to just have a few specific files handled as PHP.
RewriteEngine On
# Redirect sites with no extension to .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule .* %{REQUEST_URI}.php
This is what I came up with. This is based off of the accepted answer and its comments.

.htaccess works locally but not on server

I have apache & PHP on my local windows machine and my .htaccess rules work fine. I uploaded all my files to my Linux server and get different results. I have complete control of my Linux VPS. It isn't shared hosting or anything.
I created a file showme.php that all requests should go to. showme.php just outputs some $_SERVER variables just so I know it worked. But every request to the server gives me the 404 not found. The same request locally serves up showme.php like it should.
-Local example: http://localhost/somepage (I get the correct page rendered by showme.php)
-Server example: http://mydomain.com/somepage (I get a 404 message)
Here is my .htaccess file. I can't figure out why I get different results. Something with file permissions?
# AddType x-mapp-php5 .php
# AddHandler x-mapp-php5 .php
RewriteEngine on
Options +FollowSymlinks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . showme.php [L]
ErrorDocument 404 /page-unavailable/
<files ~ "\.tpl$">
order deny,allow
allow from none
deny from all
</files>
If you use phpinfo() and do a search for mod_rewrite (usually under configuration) you will be able to tell if you have the ability to use RewriteRules. This is usually why you see IfModule wrapped around your rewrites:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . showme.php [L]
</IfModule>
The answer was that in my httpd.conf I needed AllowOverride All set in one certain place. It was there in certain directories but I guess not as default..

Categories