How to force hide .php extension with .htaccess - .htaccess

Hi everyone this is my .htaccess file code
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
It works perfectly but can I force hide the .php extension, so even if I went to www.example.com/foo.php I get redirected to www.example.com/foo
Is there any way I can do that?

Add this to your htaccess file:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)\.php
RewriteRule ^ /%1 [L,R=301]
This matches against a request for a php file, and then redirects the browser to the same request but without the .php extension.

Related

htaccess redirect www to non-www without file extension .php

I am trying to redirect with .htaccess from non-www to www . Redirection works fine, however the .php extension of the URI is shown when redirecting from non-www to www. For example mydomain.com/info becomes www.mydomain.com/info.php
How can I get rid of the .php extension when redirecting? Thanks!
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
it works with that code to remove the .php extension also after redirecting
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L,NE]

write htaccess to hide html file extension + make http go to https + make xyz.com go to www.xyz.com

I have the following code in a htaccess file.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.domain-name.com/$1 [R,L]
I don't know anything about htaccess files but this appears to do the following:
• make domain-name.com go to www.domain-name.com
and
• make http://www.domain-name.com go to https://www.domain-name.com
so everything is going to https://www.domain-name.com Which is what I want.
However how do I also hide the .html file endings? So domain-name.com/about.html becomes domain-name/about
I've found the following code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
But don't know how to combine the two bits of code?
You can have these rules in your Apache config or site root .htaccess:
RewriteEngine On
# add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
## hide .html extension
# To externally redirect /file.html to /file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
# To internally rewrite /file to /file.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

Remove URL trailing slash and redirect

I need some help with editing .htaccess file.
need to remove trailing slash from URL, for example: website.com/file/ to website.com/file. Also redirect to website.com/file when typed website.com/file/
redirect to website.com/file, when user type website.com/file.php/ or website.com/file.php
resolve HTML anchor IDs like website.com/file.php#anchor to website.com/file#anchor or better website.com/file/anchor. Is that second possible?
I tried to remove .php extension from URL and now my CSS/JS files are not working. Is possible to use relative paths to link CSS/JS or just absolute?
My .htaccess file now looks like this:
RewriteEngine On
ErrorDocument 404 https://website.com/404
# Redirect to https
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
# Redirect to www
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ https%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Remove .php extension from URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Any ideas?
Are there some .htaccess "trics" used on websites?
Thanks!

SSL and remove extension .html via HTACCESS without redirect chains

I see that all these scripts for removing .html extension seem not to work via SSL. I solved the issue with new script but now i am creating a 2 step redirect chain which i do not like for SEO reasons - and with your help :) i hope to get it back to a redirect step / hop.
My script is
RewriteBase /
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.carpro.ro/$1 [R=301,L]
# Force WWW prefix
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^([^.]+)\.([a-z]{2,4})$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Remove .html extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.html
RewriteRule (.*)\.html$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
but it seems to create a cascade of redirects.
Is there any better version which
301 http to https for all pages of the site
redirect non WWW to WWW
remove .html extension and do not leave a trailing /
The script above works but with redirect cascades which i do not like
http://www.domain.com/something.html
does a 301 Redirect
https://www.domain.com/something.html
then again a 301 Redirect
https://www.domain.com/something
rather than
http://www.domain.com/something.html
a single redirect to
https://www.domain.com/something
Any ideas on optimising this?
Try this code:
RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://www.carpro.ro%{REQUEST_URI} [NE,R=302,L]
# Force WWW prefix
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=302,L]
# Remove .html extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.html
RewriteRule (.+?)\.html$ /$1 [L,R=302,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+?)/?$ $1.html [NC,L]
You can try below Rewrite Rules to eliminate html extension
RewriteRule ^(.*).html$ /$1 [R=301,L]
And the following rewrite rule for trailing / removal
RewriteRule ^(.*)/$ /$1 [R=301,L]
Hope this helps

.htaccess .html redirects

I've been asked to set up a redirect on a website.
Currently the website redirects allows you to visit http://www.site.co.uk/test instead of /test.html
The code being used is:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
I was wondering if anyone knows a way to keep this behaviour, but also redirect the .html extension to the non html version?
Try adding the following to your htaccess file in the root directory of your site.
RewriteEngine on
RewriteBase /
#redirect .html version
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ (/[^\ ]+)\.html\ [NC]
RewriteRule ^ %1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]

Resources