Home » Php » unable to hide .php extension. It worked only if i typed manually in url, How to fix it with .htaccess

unable to hide .php extension. It worked only if i typed manually in url, How to fix it with .htaccess

Posted by: admin October 26, 2017 Leave a comment

Questions:

My problem is that when I type the URL without .php extension (like as https://example.com/register) it worked, but when I click register link it redirect to https://example.com/register.php. I am trying to edit .htaccess file so that by default when I click register link URL should be without .php extension.

My go daddy .htaccess file code is

Options +MultiViews
RewriteEngine On 
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Answers:

Try this htaccess

RewriteEngine On

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

Use another if not work

RewriteEngine On
# remove the index file
RewriteCond %{THE_REQUEST} ^.*/index
RewriteRule ^(.*)index$ http://www.example.org/$1 [R=301,L]  

# remove the .php extension 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(([^/]+/)*[^.]+)$ $1.php [L]

# redirect from .php to less php
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php http://www.example.org/$1 [R=301,L]

Questions:
Answers:

apply this to your htaccess file

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^.]+)$ $1.php [NC,L]

it works for all(.php or no .php)

Questions:
Answers:
Options +MultiViews

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

You’ve enabled MultiViews (part of mod_negotiation) and you are also trying to append the file extension using mod_rewrite. You should do one or the other, not both. They both do essentially the same thing. MultiViews is probably “Winning”, or worse, creating a conflict.

Your existing code is essentially the same as:

Options +MultiViews
RewriteEngine On 
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Leave a Reply

Your email address will not be published. Required fields are marked *