How do i render .php URL with .html extension - php

Hi i am using Codeigniter PHP framework.
Is there is any way to display .html extension rather than .php extension in URL?

Set your url suffix on your config.php found here
/application/config/config.php
$config['url_suffix'] = '.html'; //# line 60ish

I haven't actually tried it myself but see the section Adding a URL Suffix in this part of the user guide.

Just add following to Apache's config and it will process .html files with PHP:
AddType application/x-httpd-php .phtml
With that you can rename your .php files to .html.
Or you can use mod_rewrite to handle this on the fly

use a .htaccess file that does a url rewrite .html to the equivalent .php
if you are using codeigniter you should even be thinking of hiding any extension altogether.

If ur using APache. You can use URL Rewrite Rule.
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

As others said, creating a .htaccess file will allow you to rewrite .php to .html. The following code should do the trick:
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\.html$ $1.php [L]
I am interested as to why you actually want to do this though. Codeigniter, by default, uses a segment based approach which is friendly and does away with query strings and file extensions. Using another htaccess rule, you can remove index.php from the url to make them even cleaner:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
So your urls can look like this: example.com/home, example.com/about etc. This is much prettier than having a file extension in the url.
More information about codeigniter URLs can be found here: http://codeigniter.com/user_guide/general/urls.html

Related

I am redesigning a PHP website in HTML. Do I need to set up redirects because of the file extension?

I am working on redesigning a website that is currently a PHP website. The new site will be all HTML. I am trying to keep all of the slugs the same. Should I set up redirects?
Currently, the website pages have the .PHP extension in the browser.
Example: https://www.dehartsystems.com/residential.php
The new page will be HTML and the URL will have no file extension.
Example: https://www.dehartsystems.com/residential
Do I need to set up redirects as the file extension will be changing?
To answer your question, yes you do need to set up redirects. Typically this is done in the .htaccess file in the root of your html folder on your server.
In this .htaccess file you put:
# Check rewriting is possible.
<IfModule mod_rewrite.c>
# Turn on Rewrite engine
RewriteEngine on
# check if file does not exist.
RewriteCond %{REQUEST_FILENAME} !-f
# check if folder does not exist.
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite the given URL.
RewriteRule ^(.*).php$ $1.html [R=301,L]
</IfModule>
This will redirect any call to any .php file that does not exist to the HTML file with the same file name, in the same folder location.
Reference:
https://htaccessbook.com/add-remove-change-file-extensions-htaccess/
See also How to change the PHP file extension using .htaccess file on GoDaddy Linux Hosting?
Yes, you will need to rename every file to .HTML if you don't do so the browser won't recognize the file as code. " https://www.dehartsystems.com/residential.html " only then your website will work., i.e you should rename residential.php to residential.html

Redirect file extension (.htaccess)

I was wondering if it were possible to redirect certain ending file types to another extension. It's been done before on a site I visited, but I am unable to find out how.
For exmaple:
If my website had php files and the extension was www.example.com/testfile.php and I want it to show up on the URL as www.example.com/testfile.aspx, how would I do that?
Thanks
You could do something like this:
RewriteEngine On
RewriteRule ^(.*)\.aspx$ $1.php [L]
or if you would prefer to actually save the files as aspx on the server instead of redirecting to a PHP script, do something like:
AddHandler application/x-httpd-php .php .aspx
Use this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([^/]*)\.aspx$ $1.php [L]
</IfModule>

PHP files without extensions

Is it bad to use:
ForceType application/x-httpd-php
and to save files without a file extention (e.g. index instead of index.php)? The intention is to hide/remove .php from the URL and to stop users from manually putting e.g. /example.php.
To remove the file extension, add this to the .htaccess :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
change .php to the proper file extension
new link :
link text
Edit :
Save your files as index.php, about.php, and so on
Yes. Yes it is bad.
The right way to do that is by using mod_rewrite and .htaccess files.
Its not good practice to change extension as it will need configuration for web server each time and so its a portability issue.
You should use .htaccess directives to setup any level of customization.
And in best practice you can route all requests to index.php to avoid direct access of php files.

Change url by htaccess file

I want to remove the .php extension from the url by htaccess file . please let me know How can I do it ??
url:http://domain name/profile.php
I want like this
url:http://domain name/profile
You need to read up on it in order to be able to write regular expressions and use .htaccess in different ways, but a simple rewrite would look like this:
RewriteEngine on
RewriteRule ^profile$ profile.php [L]
If you wanted to globally remove all of .php extensions but retain the filename, you'd need a regex that looked something like:
RewriteRule ^([a-zA-Z0-9\-_]+).php$ $1.php [L]

How to redirect in url by using .htaccess in php?

How to redirect in url by using htaccess in php?
http://www.example.com/randomstring/2
i would like to go to id page 20 thank
You should use mod_rewrite for example in this way:
RewriteEngine On
RewriteRule ^(randomstring)/([0-9]+)$ /randomstring/$1/ [QSA,R]
RewriteRule ^(randomstring)/([0-9]+)/$ index.php?page=$1&id=$2 [QSA,NC,L]
If you want to simply redirect from a domain to another just write:
RewriteRule ^(randomstring)/([0-9]+)$ http://www.example.com/$1/$2/ [QSA,NC,L,R=301]
use mod_rewrite, it's a module for apache. You can see the documentation here. .htaccess files control the webserver, not php, so you have to look there.
Copy that into your .htaccess file in the directory you need it:
RewriteEngine On
RewriteRule ([A-Za-z0-9]+)/([0-9]+)$ index.php?id=$2
www.domain.tld/asdf/2 --> index.php?id=2
www.domain.tld/asdfa923als/52 --> index.php?id=52
;-)

Categories