How to remove php extension form URL .htaccess - apache

I have a code in my .htaccess file:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
It removes .html extension from urls, how do I remove .php as well with the same code?

Check this modified rule
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.(php|html|htm)\ HTTP/
RewriteRule ^(.*)\.(php|html|htm)$ /$1 [R=301,L]

I am sure there's a shorter way to solve this out there but in this is how I remove php extension at the moment:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1\.php
Update:
Try this since you would like to main existing code
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.php\ HTTP/
RewriteRule ^(.*)\.php$ /$1 [R=301,L]

Related

How do I remove '.php' extension from URL without stopping the php from executing?

I have removed the '.php' file extension the URL in the .htaccess file, however, it seems to prevent my php code from running.
Could someone please suggest a way that I can get this to work?
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
RewriteCond %{THE_REQUEST} \s/+(.+?)index[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
This will internally redirect:
https://yourwebsite.com/hello -> hello.php
https://yourwebsite.com/world -> world.php
But it will only redirect if the PHP file exists.

dynamic url change issue

I want to change my URL from https://website.com/free?s=new-check to https://website.com/free/new-check/
My Current htaccess code :
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
RewriteCond %{THE_REQUEST} /free\?s=([^\s]+) [NC]
RewriteRule ^ /%1? [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ /free?s=$1 [NC,L]
It works and directed to https://website.com/new-check but what I want is https://website.com/free/new-check/
previously I removed the .php extension from the URL.
I tried a lot of solutions but nothing works. please help me
With your shown samples, please try following htaccess Rules. Make sure to keep these Rules at top of your htaccess file if you have other Rules also in your file.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine On
##https apply rules here.
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
##External redirect Rule here.
RewriteCond %{THE_REQUEST} \s/(free)\?s=([^\s]+)\s [NC]
RewriteRule ^ /%1/%2? [L,R=301]
##Internal rewrite rule here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*)/(.*)/?$ free.php?s=$1 [NC,QSA,L]

301 redirect doesnot work in htaccess

I want to redirect from https://*****.com/lp/index.html
to https://*****.com/lp/
so I put these two line in .htaccess
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /lp/index\.html\ HTTP/
RewriteRule ^/lp/index\.html$ /lp/ [R=301,L]
and now the whole redirect block in my htaccess file is like this:
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ /$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /lp/index\.html\ HTTP/
RewriteRule ^/lp/index\.html$ /lp/ [R=301,L]
but the index.html redirect not working.
Does anyone know why? Thank you.
Use it like this:
RewriteEngine On
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index\.php$ /$1 [L,R=301,NC,NE]
# remove /lp/index.html
RewriteCond %{THE_REQUEST} \s/+lp/index\.html [NC]
RewriteRule ^ /lp/ [L,R=301,NE]
Clear your browser cache and retest.
try this
http://www.rapidtables.com/web/tools/redirect-generator.htm
with Apache .htaccess redirect option

Rewrite htaccess rule for a specific file

How can I have rewrite htaccess rule so that a specific file(terms.php) with extension goes without extension(terms) like below
xyz.com/terms.php --> xyz.com/terms
RewriteCond %{REQUEST_URI} ^/terms\.php [NC]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+(terms)\.php[?\s] [NC]
RewriteRule ^ %1 [R=302,L,NE]
RewriteRule ^(terms)/?$ $1.php [NC,L]

Redirect htacces links

I want to redirect some links for not having duplicate content on google
The links are like this.. www.example.ro/adidasi_dama.php?page=1
I made the rewrite rule to .. www.example.ro/adidasi-dama/pagina-1/
with this code..
RewriteRule ^([a-zA-Z]+)([-_]){1}([a-zA-Z]+)/pagina-([0-9]+)/$ $1_$3.php?page=$4
RewriteRule ^([a-zA-Z]+)([-_]){1}([a-zA-Z]+)/$ $1_$3.php
RewriteRule ^([a-zA-Z_]+)/pagina-([0-9]+)/$ /$1.php?page=$2
But now... both versions are working... .. i want to automaticaly redirect from 1st link to second... but i need it for all my pages..
i have many links for example
www.example.ro/noutati.php?page=1
www.example.ro/adidasi_fete.php?page=1
www.example.ro/pantofi_barbati.php?page=1
...
Thank you very much!
Try adding these rules to the same htaccess file:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([a-zA-Z_]+)\.php\?page=([0-9]+)
RewriteCond %1:%2 (.+)_(.+):(.*)
RewriteRule ^ /%1-%2/pagina-%3/? [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([a-zA-Z_]+)\.php\?page=([0-9]+)
RewriteRule ^ /%1/pagina-%2/? [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([a-zA-Z_]+)\.php(\ |$)
RewriteCond %1 (.+)_(.+)
RewriteRule ^ /%1-%2/? [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([a-zA-Z_]+)\.php(\ |$)
RewriteRule ^ /%1/? [L,R=301]