htaccess remove .html from url not working - apache

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]

Related

Rewrite URL using .htaccess by replacing /? with?

How can I rewrite URL (i.e. remove last / after test) using .htaccess on php page from
from www.example.com/test/?sku=23456&qty=3 to www.example.com/test?sku=23456&qty=3
from www.example.com/page2/?page=3&emp=543 to www.example.com/page2?page=3&emp=543
from www.example.com/stream/?start=4&id=tdfcs45s&q=sat to www.example.com/stream?start=4&id=tdfcs45s&q=sat
I tried but it doesn't work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
With your shown samples and attempts please try following .htaccess rules file. We need to use THE_REQUEST variable here of apache. Check this if this helps you, also clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s(/test)/(\?sku=(\d+)&qty=\d+)\s [NC]
RewriteRule ^ %1%2 [L]

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]

HTACCESS Rewrite Rule for Query String in PHP

I have my URL https://example.com/?hfgshdsds.
I need to rewrite a rule to makes that even If I removed the question mark , the link will works as same as before, so my url need to be https://example.com/hfgshdsds.
For the moment on my .htaccess file I have only the rule that open the php without extension .php .
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
Thank you!
With your shown samples, please try following htaccess Rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine On
##keep redirection Rules first then keep rest of rules.
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php/?$ %{REQUEST_FILENAME}.php [QSA,L]
##Adding new rule here for non-existing pages here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?$1 [QSA,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 file extension with exception

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