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

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]

Related

.htaccess SEO friendly URL with queryString

I have a page like:
page.php?mode=dashboard&hl=en
and i want to use like:
site.com/dashboard?hl=en
how must i use RewriteRule? Thanks...
EDIT: I think this is not possible with .htaccess. Need to use javascript.
wish it helps you
# Redirect /Category.php?Category_Id=10&Category_Title=some-text
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^Category_Id=(\d+)&Category_Title=([\w-]+)$
RewriteRule ^Category\.php$ /Category/%1/%2? [R=301,L]
You may be able to use this code in site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} /page\.php\?mode=([^\s&]+)&(hl=[^\s&]+) [NC]
RewriteRule ^ /%1?%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w-]+)/?$ page.php?mode=$1 [L,QSA]

How do I hide a /public/ directory in htacess

My website is website.com/public/login.php. That looks a bit ugly. How do you rewrite htaccess to say website.com/login.php?
I've tried
RewriteEngine On
RewriteBase /public/
RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
The RewriteRule that strips off the /public/ path is currently an external redirect, because of the R=. Since you want the result of that rule to be hidden, it should rather be an internal redirect (also called URL forwarding), and that is written without the R=302 option.
While you're at it, you could also hide the .php from the externally visible URLs since a simple /login looks much cleaner than /login.php.
Solved. It hides /public/
website.com/login.php instead of
website.com/public/login.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public/$1 [L]
</IfModule>

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

"Pretty" URLs Not Working

I'm trying to remove the ".php" extension from the URLs of a site using this code (which I admit I copy/pasted from other questions here) in the .htaccess file:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Other lines of the .htaccess file do work, for instance, I have an error redirect and:
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
So, I know the .htaccess file is in service in general.
I don't know what could go wrong in this area, so I'm not sure where to begin troubleshooting. Does anyone have pointers?
Thanks in advance.
Given that your domain account is /home/youraccount/public_html, your .htaccess would be inside the public_html folder with the following content:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
# First we redirect the www to non-www
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^/?(.*)$ http://domain.com/$1 [R=301,L]
# now we redirect phpless URLs internally to the php
# if folder does not exist
RewriteCond %{REQUEST_FILENAME} !-d
# but the file exists and ends with .php
RewriteCond %{REQUEST_FILENAME}\.php -f
# redirect to name.php
RewriteRule ^([^/]+)/?$ $1.php [L]
NOTE: If you have more rules this may conflict so I would have to look at the rest of your rule but basically the above should work as expected.
You will be able to access both:
domain.com/index
and
domain.com/index/
And it would redirect to your file index.php.

combine two .htaccess rewrite files

I have two different .htaccess files each doing different things and when i combine them they won't work together.
the first one allows me to enter www.mydoman.com/test and have it mean www.mydomain.com/index.php?page=test
RewriteRule ^([A-Za-z0-9-_]+)/?$ index.php?subclass=$1 [NC,L]
RewriteRule ^.* index.php [NC,L]
The second file cuts off the extension from .html and .php files so can use www.mydomain.com/about insted of www.mydoman.com/about.php
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /cl_clone
## 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]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
# e.g. example.com/foo will display the contents of example.com/foo.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
Is there a way to allow both of these to work together in one .htaccess file?
You have a redirect rule at the top, and then 2 sets of rules to add either the php or html extensions to the end of the request, if such a file exists. Then your other rule does routing for index.php.
Some things you'll need to work out. You have the rewrite base /cl_clone which doesn't seem like it's going to work at all, in either of the cases, because your URLs don't have cl_clone in the request (e.g. they are simply www.mydomain.com/about, no cl_clone).
So you probably want to remove that.
Otherwise, you can simply add the index.php routing rules at the very bottom of the other rules that you have. You may need to add additional tweaking, perhaps:
RewriteRule ^([A-Za-z0-9-_]+)/?$ index.php?subclass=$1 [NC,L]
RewriteCond %{REQUEST_URI} !index.php
RewriteRule ^.* index.php [NC,L]
Or changing your RewriteBase /cl_clone to RewriteBase /

Resources