Remove file extension with .htaccess: Error with trailing slash - html

I want to remove the file extension like .html from my websites with .htaccess. The final structure should be like so:
http://domain.com/file --> http://domain.com/file.html
http://domain.com/file/ --> http://domain.com/file.html
With my existing code in .htaccess I'll get "Internal Server Error" on my Browser when there's a trailing slash at the end. What can I do? Thanks!
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9-_]+)/?$ $1.html [L]

I suggest you to change your RewriteCond :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)(\.html){0}$ /$1.html [L]
EDIT : rule edited, I forgot infinite loop.

Related

How do I remove the .html from the end of URL while using SiteGround

I had originally built my website in React before learning that my client is using SiteGround so I had to change everything over to a basic HTML format. I have my navbar using href = "fileName.html" to navigate the page. This leads the URL to become www."siteName".com/"fileName.html" once navigating off the homepage.
This is what I have so far in my .htaccess to remove the .html from the end:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule (.*) $1.html [L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]
</IfModule>
This does not seem to be working. Should I change my .htaccess file? Should I change the way my navbar navigates the pages?
With your shown samples/attempts, please try following .htaccess Rules file. Please make sure to clear your browser cache before testing your URLs.
Options -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine ON
RewriteBase /
###Rules for handling .html to without html uri in browser.
RewriteCond %{REQUEST_URI} \s/([^.]*)\.html\s [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC]
RewriteRule ^(.*)/?$ $1.html [L]
##Rule from OP's already existing .htaccess file.
RewriteRule ^index\.html$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]
</IfModule>

.htaccess - hide folder and .html

My folder structure looks like this:
domain.com/html/page.html .
If I click on a link on the page I want the URL to look like this: domain.com/page .
I tried out different .htaccess snippets but nothing worked the way I want. At the moment you can visit domain.com/html/page and you see the right page, but if you click on a link the URL is domain.com/html/page.html .
My .htaccess:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /$1.html [QSA,L]
I hope, someone can help me, thanks in advance!
You can put this code in your htaccess
Options -MultiViews
RewriteEngine On
RewriteBase /
# Redirect /html/some-page.html to /some-page
RewriteCond %{THE_REQUEST} \s/html/([^/]+)\.html\s [NC]
RewriteRule ^ %1 [R=301,L]
# Rewrite /some-page to /html/some-page.html if it exists
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/html/$1\.html -f
RewriteRule ^([^/]+)$ html/$1.html [L]

mod_rewrite rule to change URL

How could I create rewrite rule in the file .htaccess to change this URL:
http://www.benessereglobale.net/beta/shtml/home.shtml
into this:
http://www.benessereglobale.net/home.shtml
Thanks
How about:
RewriteEngine On
RewriteRule ^home.shtml$ /beta/shtml/home.shtml [L,R=301]
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /beta/shtml/$1 [L]

Point image request to a higher folder thru htaccess

Is there any rule I can write to make all requests to my image folder point up one folder. I'm dealing with a lot of links so I don't want to put ../
Eg: http://hostingxtreme.com/ajax/images/logo.png.
Should become http://hostingxtreme.com/images/logo.png
Thanks
Here the code I'm using
Dosent seem to be working!
php_value register_long_arrays On
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteEngine On
RewriteRule ^/?ajax/images/logo.png$ /images/logo.png [L,R=301]
</IfModule>
In the htaccess file in your document root, add either:
Redirect 301 /ajax/images/logo.png /images/logo.png
Or if you need to use mod_rewrite:
RewriteEngine On
RewriteRule ^/?ajax/images/logo.png$ /images/logo.png [L,R=301]
I got it!
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (ajax/|ajax/.*/)images/(.*)
RewriteRule (ajax/|ajax/.*/)images/(.*) images/$2
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (ajax/|ajax/.*/)stylesheets/(.*)
RewriteRule (ajax/|ajax/.*/)stylesheets/(.*) stylesheets/$2

Trouble removing .html URL extension using .htaccess

Trying to remove .html extensions from the site using .htaccess. So for example: www.mysite.com/charts.html would become www.mysite.com/charts
The following script is in the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
But when the url without the .html extension is entered in the browser, it shows a 403 Forbidden error. Any help would be appreciated.
I found this solution elsewhere:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
sources:
1) http://www.catswhocode.com/blog/10-useful-htaccess-snippets-to-have-in-your-toolbox
2) http://eisabainyo.net/weblog/2007/08/19/removing-file-extension-via-htaccess/