0

I am working on a project where I decided to get rid of the .php extension from the URL of my app. I am successful using the htaccess code given below. But the problem is, the page still loads using .php extension if typed manually or loaded from the history.

My current .htaccess code.

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

I want it to always load with www.example.com/dashboard except www.example.com/dashboard.php even if dashboard.php is manually typed.

UPDATED .htaccess code

RewriteEngine On
RewriteBase /dateapp/
RewriteRule ^([^\.]+).php$ $1 [R]

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

2 Answers 2

1

You can add redirect as first rule.

RewriteEngine On
RewriteRule ^([^\.]+).php$ /$1 [R]

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

You have to use END flag instead of L otherwise the redirects will end in infinite loop. This will work if the .htaccess is in your webroot folder.

In case you have .htaccess in 'folder' subfolder you will have to add RewriteBase

RewriteEngine On
RewriteBase /folder/
RewriteRule ^([^\.]+).php$ $1 [R]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,END]
Sign up to request clarification or add additional context in comments.

7 Comments

it takes me to the root URL i.e., localhost/dashboard.php but my file is in localhost/folder/dashboard.php . Also, its not removing the extension.
Do you have the .htaccess in webroot or in the 'folder' subfolder?
@AurazoScript I've added a version for the .htaccess in subfolder instead of webroot.
I replaced "folder" with my folder name but when trying to access the page using the .php extension it quits and says "This page isn't redirecting properly".
@AurazoScript can you add the edited htaccess to you question? I've tested both variants and they worked fine for me.
|
0

You may use this code in dateapp/.htaccess:

RewriteEngine On
RewriteBase /dateapp/

RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,NE,L]

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.