Hide .php extension from specific files using .htacces - .htaccess

I would like to permanently redirect these urls only:
http://www.example.com/privacy.php
http://www.example.com/terms.php
http://www.example.com/contacts.php
to:
http://www.example.com/privacy
http://www.example.com/terms
http://www.example.com/contacts
Continuing to serve the contents inside corresponding .php files.
Any help would be greatly appreciated!
Thank you.
P.S. I edited my question because it has been identified as a possible duplicate of another question; the answer of the suggested question doesn't solve my problem
This is .htaccess current content:
RewriteEngine On
RewriteBase /
# Adds the www. before any URL
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Redirects index.php page to the homepage
RewriteRule ^index\.php/?$ / [R=301,L]
# Redirects all old pages example.com/?d=124575 to http://www.example.com
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /\?(.*)\ HTTP
RewriteRule ^ /? [R=301,L]

This is what I use on my website.
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [L]
And it works without a failure.

This will do it for you:
RewriteRule ^(privacy|terms|contacts)$ $1.php

Related

htaccess 301 redirect root to subdirectory

Trying to get
www.example.com
to go directly to
www.example.com/forum
How can I do this with this configuration? Thanks in advance.
Edit:
Right now I have added below content in .htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
RewriteRule ^(.*)$ /forum/$1 [L,R=301]
I want result like
https://www.example.com/topic/topic-url/
to
https://www.example.com/forum/topic/topic-url/
With your shown samples, could you please try following. Please clear your browser cache before testing your URLs. Your tried rules will be creating an infinite loop hence it may not be working, I have added a condition if uri doesn't start from forum then only proceed with redirection.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{REQUEST_URI} !^/forum [NC]
RewriteRule ^(.*)$ /forum/$1 [L,R=301]

Using .htaccess, how can i change file extension at run time

I want to make a website with PHP, but don't want to show the extension like http://example.com/index.php, i have that index.php file but want to show only index.html, may be .html something else..
I don't know how to do, please help me.
You can use these 2 rules in your site root .htaccess:
RewriteEngine on
# To externally redirect /dir/foo.php to /dir/file.html
RewriteCond %{THE_REQUEST} \s/+(.+)\.php[\s?] [NC]
RewriteRule ^ /%1.html [R=301,L]
# To internally rewrite /dir/file.html to /dir/file.php
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+)\.html$ $1.php [L]
There is a really easy way with .htaccess:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} (.*)\.php
RewriteRule ^(.*)\.php $1.html [R=301,L]

Cannot hide all extensions using htaccess

I can't get my htaccess file to hide all file extensions (html and php in my case).
If I type in 'example.com/home.html',
I want 'example.com/home' shown in the address bar.
Is this possible to do with htaccess without changing my html code to 'href="/home" '?
Thanks in advance.
Try:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+(.+)\.html(\?\ )
RewriteRule ^ /%1 [L,R=301]
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.*)$ /$1.html [L]
in the htaccess file in your document root.
Searched some more and found that you already provided a solution for this in another post.
# browser requests html
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.html
RewriteRule ^/?(.*)\.html$ /$1 [L,R=301]
# check to see if the request is for a html file:
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^/?(.*)$ /$1.html [L]

.htaccess rewrite to subfolder with codeigniter rewrite

I have a site that needs to exist in a subfolder
example.com/site
But i'm trying to use the .htaccess to remove any links that contain www (to make sure codeigniter csrf doesn't throw errors), so i've added
RewriteCond %{HTTP_HOST} ^(www\.example\.com)?$
RewriteRule ^(.*)$ http://example.com/site/$1 [R=301,L]
This works well when there is a page identifier specified, so
www.example.com/site/book rewrites to example.com/site/book
But when there is no page identifier specified I get a 404
www.example.com/site rewrites to example.com/site//usr/local/pem/vhosts/103480/webspace/httpdocs/new
I was wondering if anybody could point me in the right direction?
This is my full .htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.example\.com)?$
RewriteRule ^(.*)$ http://example.com/site/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
You may try this instead:
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{REQUEST_URI} ^/(.*) [NC]
RewriteRule .* http://example.com/%1 [R=301,L]
Maybe, you're just missing a RewriteBase
Depending on where the .htaccess file is, try either
RewriteBase /
or
RewriteBase /site
Never test with 301 enabled, see this answer
Tips for debugging .htaccess rewrite rules
for details.

.htaccess, URL changes but loading content

I'm having an issue with .htaccess where I have managed to successfully rewrite the URLs but the content is no longer loading.
The following is my htaccess file.
I'm aiming for all my .html pages (the site is made of .html static pages) to have their extensions removed. However, I require the .html URLs to 301 redirect to the new URL's so that my SEO does not take a hit from these changes.
Example:
Original: www.example.co.uk/page.html
Desired: www.example.co.uk/page/
It is important that the original URL redirects the new URL's though.
Options +FollowSymLinks
RewriteEngine on
# REDIRECT yourdomain.com TO www.yourdomain.com
RewriteCond %{HTTP_HOST} !^www.example.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [L,R=301]
RewriteRule (.+)\.html?$ http://www.example.co.uk/$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(([^/]*/)*[^/.]+)$ $1.html [L]
I have tried the htaccess above and I've also tried the variation below, but neither has worked as desired. Any help would be much appreciated.
Options +FollowSymLinks
RewriteEngine on
# REDIRECT yourdomain.com TO www.yourdomain.com
RewriteCond %{HTTP_HOST} !^www.example.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [L,R=301]
RewriteRule (.+)\.html?$ http://www.example.co.uk/$1/ [R=301,L]
You need to change 2 things.
Add this right after your redirect to prevent a redirect loop:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
change the last rule to this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1\.html -f
RewriteRule ^ /%1.html [L]

Resources