.htaccess remove file extension with exception - apache

I want to remove all the file extensions (like .php) from my URL/website and redirect to the pages without the extensions. I came up with this solution so far (in .htaccess):
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
This works fine so far.
But I need an exception rule for one site. This site should keep his file extension or else I can't send data via the POST method. The solution most users offer is this:
RewriteEngine on
RewriteRule ^todo\.php$ - [L]
But this solution doesn't work for me for whatever reason. Does anyone else has an idea?

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Try this

Related

htaccess remove .html from url not working

hi i am trying to remove .html from url.
example : from: example.com/home.html to example.com/home
i ran into some videos and posts regarding .htaccess file.
i used this code for htaccess and checked it on htaccesschecker , it says syntax is ok.
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
i am trying to do it first on vscode. when i run live server i just get same .html ending for every html file...
With your shown samples please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect rules here.
RewriteCond %{THE_REQUEST} \s/([^.]*)\.html\s [NC]
RewriteRule ^ /%1? [R=301,L]
##Internal rewrite rules here..
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ $1.html [L]

Mod Re-Write Struggling With Rule Not Working

So after a lot of testing - I've started to figure out my problems but still can't get the rewrite to work.
This is how my htaccess file looks now.
RewriteEngine On
RewriteRule ^schemdetail/id/(.*)$ schematicdetails?id=$1 [L,NC]
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://abfielder.com/$1 [R=301,L]
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
Rule 1 is the one I'm having problems with
RewriteRule ^schemdetail/id/(.*)$ schematicdetails?id=$1 [L,NC]
My understanding is now if I type this url into my browser
https://abfielder.com/schemdetail/id/158
I should get
https://abfielder.com/schematicdetails?id=158
The test of this here
https://htaccess.madewithlove.com/
Tells me my rules are ok.
And rules 2 and 3 are working fine.
However when I try and access
https://abfielder.com/schemdetail/id/158
I essentially get the page not found error.
Ok think I've finally fixed this for anyone else who is having issues with mod rewrite where they wish to have a rule to remove the file extension and other rewrites working together the order matters. Lastly if you're on a shared host type thing you may also need to set the rewrite base. This is how my htaccess file looks now.
RewriteEngine On
RewriteBase /
RewriteRule ^schemdetail/id/(.*)$ schematicdetails?id=$1 [L,NC]
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://abfielder.com/$1 [R=301,L]
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
This allows me to have url's without the .php extension.
It also allows for
schemdetail/id/100 to be translated into schematicdetails?id=100

htaccess - remove .php extension from url

I tried a lot of code to remove .php from url
for example - ht.abuena.net/presto.php -> ht.abuena.net/presto
and vice versa - internally
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
nothing works - the url stays unchanged
page is reloading by right clicking on the button Reload and choosing - Empty Cache and Hard Reload - so I hope the cache is cleared
live example here - here
With your shown samples, please try following htaccess rules file.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} ^$
##using THE_REQUEST variable for condition check.
RewriteCond %{THE_REQUEST} \s/([^.]*)\.php/?\s [NC]
##Performing external redirect here.
RewriteRule ^ %1? [R=301,L]
##Performing rewrite for non-existing pages.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)/?$ /$1.php [QSA,L]
In my case if the project is core PHP one, I use the below lines in .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

Remove .php from URL but it should still be pointing towards the original file (mod-rewrite)

Dear fellow programmers
I'm pretty new to web development. Especially with the .htacces file. So I'm developing a webpage and I want to remove the .php file extensions. It is working for html but not for the php extension if I use the same code for it.
What I'm trying to achieve is when the user gets redirected to https://website.com/pong.php the URL gets changed to: https://website.com/pong. But the URL should still point to the first URL. Same for HTML files.
Thats the code I have for removing the html extension. There is allsow a line to redirect the page automatically to the https protocol (3rd line)
Options +MultiViews
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
I tried this to remove the php with but it didn't work. This is the line I added to my code.
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I tried the answer of this question: removing .html or .PHP URL extension using .htaccess not working
But It generated this error while I tried to open the page: ERR_TOO_MANY_REDIRECTS
Thanks in advance for the help
With your shown samples, could you please try following.
Please make sure you clear your browser cache before testing your URLs.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]

htaccess remove .php extension from the following

I have this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ ./index.php?path=$1 [L,NC,QSA]
I need the .php extension removed from the browser address
This is meant to answer your comment below the question:
RewriteEngine on
RewriteRule ^(.*)\.php(.*)$ $1$2 [QSA,L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ ./index.php?path=$1 [L,NC,QSA]
However I do not really see a point in this. Where should the ".php" extension in those urls come from? I mean users won't write down a url to query your system themselves. Instead they click on a link and that link must be something you sent out before. So obviously your approach should be to send out links that do not contain that file name extension in the first place!
You can use these 2 rules for hiding .php extension:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*)$ $1.php [NC,L]