does mod_rewrite on file extensions interfere with verification files? - html

I'm currently using mod_rewrite to remove the .HTML extension from files, as well as redirect to their friendly versions.
I use the pinterest.html and google.html files for verifying my Pinterest page and Google Apps account.
For mod_rewrite, I am using this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
My question is about the logic and function of it in relation to the verification files. The purpose as I understand is to serve 'x' file with 'y' URL.
So even though I am serving example.com/pinterest.html as example.com/pinterest due to the redirection and .HTML removal, there shouldn't be any interference since the file is still readable as always, correct?

Don't rewrite the two special files. Add a negative assertion to the cond regexp:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(?!pinterest\.html|google\.html).*\.html\ HTTP/
PS. why not just match %{REQUEST_URI} ?
RewriteCond %{REQUEST_URI} !(pinterest|google)\.html$
RewriteCond %{REQUEST_URI} \.html$
I've rewritten this in simple regexp. What this says is don't match the two special files but so match any other request for a .html file.

Related

How to remove .html from URL and redirect when the html will be accessed directly

I know that I can remove the .html using the remove html .htaccess code which is this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
I still don't like this cause when visitors access my site with a .html extension then the .html will still not be removed and can still be shown by the visitors.
But I want a user to be redirected without a .html extension when he/she access the file directly. For example when a visitor wants to access file1.html directly on the browser then it automatically redirects or delete the .html extension, so instead of showing file1.html when accessed directly on browser then it will automatically rewrite "file1.html" to "file1" without the html extension anymore.
Also additional, how to add a trailing slash?
(Truly sorry if I haven't elaborate clearly.)
In your .htaccess file try adding:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.html
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
should remove .html and add a trailing /
source

Folder name clashing with html page (using htaccess)

I have a static html website and I'm using .htaccess to clean up the url by removing the .html from the address.
The issue is when I visit "example.com/path" it clashes with the folder "path" but when I choose to manually enter example.com/path.html I will get the page loading just fine.
The URLs inside the "path" folder are working fine as well with "example.com/path/page1" both with and without .html file extension.
My only alternative just now is to change the folder name from the file name as subtle as possible until I find a solution.
I've had a look at related questions being asked as well as a bit of research on conditions to put into my .htaccess file but having no luck.
Appreciate any help.
EDIT ***
htaccess file ..
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R,L]
RewriteCond %{HTTP_HOST} !^www.example.co.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk$1 [L,R=301]
Just returning a 403.
Kindly try below code
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*?)/?$ $1.html [L]
Note : Don't forget to replace RewriteBase. As of now i have added / bca my project is in root folder. You can simply add folder name like below
RewriteBase /yourfoldername/
Hope it will work for you.

from .html to no .html angular

so basically I've this htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
#RewriteRule ^(.*) /index.html [NC,L]
RewriteRule ^ index.html [L]
</IfModule>
but old website has all the .html url's like www.mysite.com/contact.html
thing is it's first page in a lot of google searches and i don't want to loose all that indexed pages, so i what i want is to get that and remove the .html
also, i've custom url's like www.mysite.com/product/whatever_my_name.html
same story, grab that and make it www.mysite.com/product/whatever_my_name
thanks in advance
RewriteCond %{REQUEST_URI} ^((?:/[^/]+)+).html$
RewriteRule ^ %1 [NS,L,R=301]
Testing only the client's request is important to avoid interference with serving any other html docs, e.g. your main index.html.

htaccess rewrite url to resource and display clean url

I have urls in the following pattern that point to static html resources:
/1-process.html or
/2-process-details.html
I am trying to rewrite the urls to exclude the numbered prefix and file extension, the above becoming:
/process/
/process-details/
I am able to rewrite the url to show in the browser the clean url, but the html resource is of course not found.
I want to retain the file naming structure but clean it up on the url side. Is this possible?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^([1-9]-)(.*)\.html$ /$2 [R=301,L]

getting html5 boilerplate .htaccess file to work on dreamhost site

Completely new to Apache.
Trying to remove ".html" extension from urls by adding the following lines (found here):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
to this (including ^ those lines now):
<IfModule mod_rewrite.c>
RewriteEngine On
# Enable the `FollowSymLinks` option if it isn't already.
# Options +FollowSymlinks
# If your web host doesn't allow the `FollowSymlinks` option, you need to comment it out or remove it, and then uncomment the `Options +SymLinksIfOwnerMatch` line, but be aware of the performance impact.
# Options +SymLinksIfOwnerMatch
RewriteBase /
# RewriteOptions <options>
RewriteCond %{HTTPS} =on
RewriteRule ^ - [env=proto:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^ - [env=proto:http]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
</IfModule>
But the .html extensions remain in the visible urls.
I couldn't find any info on whether Dreamhost allows FollowSymLinks, but I tried it enabled, as well as SymLinksIfOwnerMatch, I also tried disabling RewriteBase / and even removing the mod rewrite if statement. I'm placing the file in my website's root directory, and tried placing it in the parent folder to that too with no results. Anybody have an idea what I'm doing wrong?
The full .htaccess is here.
Solved:
I still don't know why the above code didn't work, but I just used the code from Dreamhost's wiki instead:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\ HTTP/
RewriteRule ^(.*)index$ http://Your_Site_Url_Goes_Here.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://Your_Site_Url_Goes_Here.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.+)\.html\ HTTP/
RewriteRule ^(.+)\.html$ http://Your_Site_Url_Goes_Here.com/$1 [L,R=301]
RewriteRule ^([a-z]+)$ /$1.html [L]
</IfModule>
Be mindful that the above code may cause new problems, as it is causing one of my pages to return a 403 Forbidden error, but I think that's a whole other problem irrelevant to my original question so I consider this a solve and will fix the above code when I find a solution elsewhere.
Symlinks seem to have been irrelevant.
.htaccess file goes in >mySite.com directory, rather than the directory that contains this directory
DreamHost wiki has a guide on how to use Apache redirects and rewrites. The guide mentions FollowSimylinks
You should also check the Apache error logs to see what fails (and edit your question to be more specific about the result you see in the browser).