how to remove the url extension - html

I am developing a website for myself, pure and without server language.
The urls are as they are called in the a tag, for example: blog / index.html.
Is there anything I can use to change that? I don't want to have to use framework. I would like to know if there is any tool that does this.
What I want to change is the ending. I don't want the extension to appear, just the route

try create .htaccess file on your root folder then paste this
#remove html file extension-e.g. https://example.com/file.html will become https://example.com/file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
you can read more about this here https://www.plothost.com/kb/how-to-remove-php-html-extensions-with-htaccess/
EDIT
Since you are using Vercel.com, as per their documentation there's a file config named vercel.json you can add this
{
"cleanUrls": true
}
the docs said,
When set to true, all HTML files and Serverless Functions will have their extension removed. When visiting a path that ends with the extension, a 308 response will redirect the client to the extensionless path.
for more information pls read their docs here https://vercel.com/docs/configuration#project/clean-urls

Related

Polymer: how to link to a PHP file?

I'm building a multi-page app using Polymer. In one of the components (or pages, really), I've got a link to a PHP file:
<p>Export as PDF</p>
This PHP file generates a PDF file. If I point the browser directly to it, it prompts me to save the PDF file - all good.
But when I click on the link form within Polymer, then it goes to my "page not found" component.
My .htaccess file redirects everything to Polymer's index.html, except files that do exist:
# Pass all requests not referring directly to files in the filesystem to index.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.html [L]
It actually works, since when I point the browser directly to the PHP file it executes it properly.
So, how do I tell Polymer to not treat this link as an internal router thing?
Thanks a lot for any assistance.
The solution was pretty simple, I just needed to use an absolute URL, rather than relative:
<p>Export as PDF</p>

How do I redirect "website/page" to "website/page.html" when people type it in browser (pretty-url)

I recently started using a static site with godaddy.com without a cms. It's a basic website I uploaded and is only html/js/css.
When people type "website.com/page" they get 404 not found.
When they type "website.com/page.html" they get the correct page.
How can I fix my site so users can simply type "website/page" and not be forced to type "website/page.html" without using php?
SOLUTION
After researching the information about .htaccess files provided by #Gijsberts (thank you), I did the following:
Created a .htaccess file on server.
Added the following code:
# Remove the .html extension from html files
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.html [NC,L]
Most CMS systems have a .htaccess file what handles the redirect and the 404 errors. In CMS system they are most called "Pretty-urls". Read the links below to set up those pretty urls yourself with a static web page without a cms.
https://mediatemple.net/community/products/grid/204643080/how-do-i-redirect-my-site-using-a-htaccess-file
http://code.tutsplus.com/tutorials/using-htaccess-files-for-pretty-urls--net-6049
You normally can do this with your Webserver config. In Apache you have this enabled automatically. In nginx you should have a look at this tutorial.
If you are using caddy you have to config your caddyfile like
ext .html .htm .php
if you are using microsoft iis have a look at microsofts tutorial.
Let me know if you are using something else or need more help.
EDIT
If you are using a webspace you may are not able to change the config of the webserver. you now can use .htaccess. create a .htaccess file and write down something like
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

How can I remove the .html from the end of URL pages

I want to remove the .html extensions from the end of the file names. As seen on the website: tekmillion.com
Any help will be appreciated, thanks.
Use this to remove extensions in your .htaccess.
An .htaccess file is a simple ASCII file that you create with a text
editor like Notepad or TextMate. It provides a way to make
configuration changes on a per-directory basis.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /html/(.*).html\ HTTP/
RewriteRule .* http://localhost/html/%1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /html/(.*)\ HTTP/
RewriteRule .* %1.html [L]
Checkout this reference to learn more about this.
there is a way but it is not really good. You need to change the extension of your files from .html to .php and than you need to upload them to online hosting or you need XAMPP server.
Note: If you try to run files with .php extension the browser won't load them except if you have XAMPP server.
change the name of the files to index.html and put them in separate folders
http://tekmillion.com/videos.html
should become
http://tekmillion.com/videos/index.html
and you can acees it using the link
http://tekmillion.com/videos/
and you will not see the .html

Redirect request for directory-name to directory-name.html with htaccess

On my website, when somebody requests a directory, I want it to remove the '/' and add '.html' (minus the quotes, of course).
Example:
If someone goes to domain.com/directory/ it should redirect to domain.com/directory.html/ and the same should stand for: domain.com/another-directory/ should redirect to domain.com/another-directory.html.
I would like to place a line (or two) of code to my htaccess file that will make any directory (URL ending with /) redirect to URL.html (removing the / of course).
I would also like it to visually redirect, so the user will actually see it change to .html.
I'm quite a novice web programmer, and any help is greatly appreciated.
Note: I did use Redirect /directory /directory.html and that worked, but that requires a lot of extra coding, and I would much prefer one simple statement to cover all directories.
This is going to be a bit difficult with htaccess, I assume you want to do the following:
If someone accesses a directory that isn't the root (simply http://domain.com/), redirect them to the directory name ending with .html
After the get redirected, internally rewrite the .html back to the directory so apache can serve the directory.
First one is straightforward:
# check to make sure the request isn't actually for an html file
RewriteCond %{THE_REQUEST} !^([A-Z]{3,9})\ /(.+)\.html\ HTTP
# check to make sure the request is for a directory that exists
RewriteCond %{REQUEST_FILENAME} -d
# rewrite the directory to
RewriteRule ^(.+)/$ /$1.html [R]
Second part is tricky
# check to make sure the request IS for an html file
RewriteCond %{THE_REQUEST} ^([A-Z]{3,9})\ /(.+)\.html\ HTTP
# See if the directory exists if you strip off the .html
RewriteCond %{DOCUMENT_ROOT}/%2 -d
# Check for an internal rewrite token that we add
RewriteCond %{QUERY_STRING} !r=n
# if no token, rewrite and add token (so that directories with index.html won't get looped)
RewriteRule ^(.+)\.html /$1/?r=n [L,QSA]
However, if what you just have is a bunch of files called directory.html, directory2.html, directory3.html, etc. and you want to make it so when someone enters http://domain.com/directory2/ into their address bar they get served the contents of directory2.html, it will be much simpler:
# check to make sure the request isn't actually for an html file
RewriteCond %{THE_REQUEST} !^([A-Z]{3,9})\ /(.+)\.html\ HTTP
# check to see if the html file exists (need to do this to strip off the trailing /)
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.html -f
# rewrite
RewriteRule ^(.+)/$ /$1.html [L]

How to make URLs in a website appear solely as folders

I am trying to design a folder structure for a website project I am working on. A lot of sites these days seem to have the following link structure:
www.example.com/news/news-item-one/
www.example.com/about-us/
Can I make my site work like this without making a new folder for each page I have and putting an index.php file in it?
i.e www.example.com/news/new-item-one.php reads www.example.com/news/news-item-one/
You can use a web application framework like CodeIgniter or CakePHP to do URI routing for you:
http://codeigniter.com/
http://cakephp.org/
This is done with an .htaccess file which either of those frameworks can provide in example documents and they have extensive documentation about URI routing. For example on CI:
http://codeigniter.com/user_guide/general/urls.html
Yes, but it depends what server you are running.
If Apache, one of the most common ways is to create an .htaccess file and use rewrite rules to declare the different routes your website uses.
Below is a very simple example, although not necessarily the best way. There are things you can do to make it more flexible, but I believe it's out of scope of this question. For what it's worth, I prefer a catch-all route that passes route handling to my framework.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^news/(.*)$ news.php?item=$1 [NC,L,QSA]
RewriteRule ^about-us/$ about-us.php [NC,L,QSA]
</IfModule>
You want to use htaccess.
Create a file in your root directory called .htaccess with the following
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)$ $1.php [NC]
You don't need to use a framework (although it may be a good idea). You can simply setup your .htacess correctly:
http://www.evolt.org/Making_clean_URLs_with_Apache_and_PHP
The following mod_rewrite code (to put in your Apache configuration) will allow you to hide the .php extension of any page you have on your site.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^(.*)$ $1.php [L]
It checks to make sure something isn't a file or directory itself, then that adding .php after it's name actually is a file, and it serves that instead.
So if you have a /page.php on your site, going to /page will be the same as going to /page.php.