If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Eliminate the extension .html without employing php?

Started by wilomr11st, Apr 11, 2023, 12:01 AM

Previous topic - Next topic

wilomr11stTopic starter

After investigating how to eliminate the ".html" extension from webpage addresses via a .htaccess file, I discovered that all of the available code was written in PHP.

However, since I am using free hosting with restrictions on the use of PHP, I am unable to utilize this method. Consequently, I am wondering if there is any alternative approach for removing the file extension without relying on PHP - perhaps something like JavaScript?
  •  


sujitbikash

Rewrite Engine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.html\ HTTP
RewriteRule ^([^.]+)\.html$ site.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule(.*) /$1.html
site.com replace with the domain of your site.


I highly recommend using the option to redirect from /about.html to /about in the .htaccess file. This is something I personally use and find very effective.
  •  

ElizabethParker

The given code activates the Rewrite Engine and checks if the file path mentioned in the URL exists. If it does not exist, then it appends the .html extension to it.

Code:
Rewrite Engine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

Explanation:
#Turn on the conversion mechanism
Rewrite Engine On
#Set the condition "is the file path specified in the url?"
RewriteCond %{REQUEST_FILENAME} !-f
#If yes, then just remove the html
RewriteRule ^([^\.]+)$ $1.html [NC,L]


The path is specified in the code as <a href="example">Example</a>. Additionally, instead of $1.html, one can also use $1.php.
  •  

DusFriesteLet

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^.*$ $0.php [L,QSA]
RewriteCond %{THE_REQUEST} ([^\s]*)\.php(\?[^\s]*)?
RewriteRule (.*) %1 [R=301,L]


This code has been verified and is effective for individuals who require the elimination of the php extension from the address bar.
  •  

Emily Evans

When it comes to removing the ".html" extension from webpage addresses without relying on PHP due to hosting restrictions, it's important to explore alternative server-side methods such as mod_rewrite in Apache.

First, let's take a closer look at using mod_rewrite in your .htaccess file to achieve this. The following example demonstrates how to rewrite URLs to hide the ".html" extension:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+)$ $1.html [NC,L]


In this code, the RewriteEngine directive enables the URL rewriting engine, and the RewriteCond directives are used to check if the requested file or directory exists. If the file exists and the request does not end with a slash, the RewriteRule then appends ".html" to the requested URL.

This approach effectively hides the file extension from the URL while serving the corresponding HTML content. It's a powerful server-side technique that doesn't rely on PHP, making it well-suited for environments with restrictions on PHP usage.

Now, let's touch on the client-side aspect. While JavaScript alone cannot directly manipulate the server's URL structure, it's worth considering how JavaScript can enhance the user experience in handling URLs.

For instance, client-side JavaScript can be used to dynamically modify and update the displayed URL in the user's browser, creating a smoother, more seamless browsing experience. This could involve leveraging JavaScript to update the URL displayed in the address bar as users navigate through your website.

However, it's important to note that while client-side JavaScript can enhance the user interface and experience, it doesn't directly remove the file extension from the server's URL structure. Server-side solutions like mod_rewrite remain the primary method for achieving this.
When faced with hosting restrictions on PHP usage, it's worthwhile to explore server-side technologies such as mod_rewrite to achieve clean, extension-less URLs. While client-side JavaScript can complement the user experience, server-side solutions are essential for fundamentally altering URL structures.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...