Cannot hide all extensions using htaccess - .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]

Related

Clean URLs in a subfolder

I've read and followed guides and looked for answers to other people's questions, but I'm struggling with url rewriting and htaccess.
I have a subfolder on my site with an index page which handles query strings to bring up dynamic content. I'd like the variable to appear to be a subfolder name e.g.
http://mywebsite.com/subfolder/index.php?v=variable
to
http://mywebsite.com/subfolder/variable
The .htaccess file I've made is at http://mywebsite.com/subfolder/ i.e. the same folder as index.php
How can I get this to work? Any help gratefully appreciated.
You can use these rules inside your /subfolder/.htaccess
RewriteEngine On
RewriteBase /subfolder/
RewriteCond %{THE_REQUEST} \s/subfolder/index\.php\?v=([^&\s]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ index.php?v=$1 [L]
Update (passing two values)
RewriteEngine On
RewriteBase /subfolder/
RewriteCond %{THE_REQUEST} \s/subfolder/index\.php\?v=([^&\s]+)&v2=([^&\s]+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
RewriteCond %{THE_REQUEST} \s/subfolder/index\.php\?v=([^&\s]+)\s [NC]
RewriteRule ^ %1? [R=301,L]
# Don't touch to existing files/folders
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Rewrite /xxx to /index.php?v=xxx
RewriteRule ^([^/]+)$ index.php?v=$1 [L]
# Rewrite /xxx/yyy to /index.php?v=xxx&v2=yyy
RewriteRule ^([^/]+)/([^/]+)$ index.php?v=$1&v2=$2 [L]
Use this in your .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} v=(.+)$ [NC]
RewriteRule ^ subfolder/%1? [R=301,L,NE]
This grabs the variable using %{QUERY_STRING} and then appends it to the rewrite using %1. You'll see I've added a ? onto the end of the rewrite. That is to stop the original query from appearing on the end of the URL.
I've used R=301 which is a permanent redirect. You might want to changes this to R=302 while you're testing, as this is temporary.
You can view a test of this rule working here: https://htaccess.madewithlove.be?share=7b6832e9-2c05-5d1d-916c-e4dd0f5b1da6
Make sure you clear your cache before testing this.

Hide .php extension from specific files using .htacces

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

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]

.htaccess mod_rewrite cannot remove page name

Having trouble figuring out the mod rewrite for .htaccess I want the url http://www.example.com/archive.php?title=about_me which is a dynamic url to be rewritten to http://www.example.com/about_me. I am using php and here is my current .htaccess code, however it only rewrites to http://www.example.com/archive/about_me want the archive to be removed.
Options +FollowSymLinks -MultiViews
rewriteengine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/archive\.php\?title=([^&\ ]+)
RewriteRule ^ /archive/%1? [L,R=301]
RewriteRule ^/?archive/(.*)$ /archive?title=$1 [L]
## Hide .php extension
## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
I did get it to rewrite correctly with this code
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/archive\.php\?title=([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]
RewriteRule ^/(.*)$ /archive?title=$1 [L]
However it then returns a page cannot be found error
I you want the /archive/ to be removed, you'll have to ensure that any URI that's in the form of /something must absolutely be routed to the archive.php script. Because there's simply no way to tell whether /my_blog is actually a request for /my_blog or whether it needs to be sent to the archive.php script with "my_blog" as the value of title in the query string. The best you can do is check that it's not a request for an existing resource via the -f and -d conditions:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/archive\.php\?title=([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]
# no /archive/ ^
# condition checks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ /archive?title=$1 [L]
Something like this should do the trick:
RewriteCond %{QUERY_STRING} ^title=(.*)
RewriteRule ^archive.php /%1?
RewriteRule ^(.*)$ /archive?title=$1 [L]
EDIT: Added the final RewriteRule as noted in my comments on the original question. Per the comment I believe you are trying to do the following two things:
Redirect any user-entered "real" URLs to the "friendly" URL: http://www.example.com/archive.php?title=about_me to http://www.example.com/about_me as stated in the question.
Rewrite the "friendly" URL to the "real" URL: http://www.example.com/about_me to http://www.example.com/archive.php?title=about_me, which was not clear as stated.

.htaccess mod_rewrite Convert file to php and don't show filetype

Given a set of URL as follows:
http://site.com/filename.html
http://site.com/filename.htm
http://site.com/filename.php
http://site.com/filename
Using the mod_rewrite module in .htaccess, how can I make it request the file at
http://site.com/filename.php
and show the URL
http://site.com/filename
This should satisfy your requirements:
RewriteRule ^([^\.]+)$ /$1.php [L]
EDIT: After your comment and changes in the question - try this:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\. /$1 [R,L]
RewriteRule ^([^\.]+)$ $1.php [L]
Try these rules:
# remove file name extension and redirect externally
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]+\.(html?|php)
RewriteRule ^([^.]+)\.(html?|php)$ /$1 [L,R=301]
# rewrite to PHP file internally
RewriteRule ^[^.]+$ $0.php [L]

Resources