force SSL for single .html page without php or anything else just using .htaccess - apache

After several hours of trying a myriad of suggestions for .htaccess I have given up and decided to ask here.
I have a single html page that needs to be served via SSL. It is a single file with the .htm extension and it contains no php whatsoever. If anybody accesses this page via typing it in or clicking on a link from a non SSL page, I want that person to be redirected to or shown the SSL version of that page. Only https://example.com/myfile.htm should be allowed. The rest of the site can go without SSL, just this one page needs it.
Please help.

Try this in your .htaccess file.
RewriteEngine On
RewriteCond %{HTTPS} !^on$
RewriteRule ^myfile\.htm$ https://www.example.com/myfile.htm [R=301,L]

Try:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^myfile\.htm$ https://example.com/myfile.htm [L,R]

SSL for HTML only page / SSL for JavaScript only page:
If anyone needs to set SSL (HTTPS) for page that uses html only (without PHP, nodeJS etc.) just put .htaccess file in the same folder as index.htm page.
Content of the .htaccess have to be:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I had trouble finding how to, so I think that this thread is the best to hold this answer.

Related

How redirect a page with htaccess and relative requests?

I need to type on browser
mydomain.me/myproject
instead to type
mydomain.me/fold1/fold2/yymmdd-myproject
It works after I've added the code below in .htaccess:
RewriteEngine On
RewriteRule ^myproject/?$ fold1/fold2/yymmdd-myproject/ [NC,L]
But now the requests from the index page (css, script sources...) call them from the domain's root (mydomain.me, not from mydomain.me/myproject/) and, of course, the server responds with "not found".
How can I fix it?
UPDATE 1
I tried to write this (just added the domain in path):
RewriteEngine On
RewriteRule ^myproject/?$ http://mydomain.me/fold1/fold2/yymmdd-myproject/ [NC,L]
and now the page loads correctly with css and other libraries, but the url in address bar is mydomain.me/fold1/fold2/yymmdd-myproject/ instead like mydomain.me/myproject as I read in Apache document.
I solved in this way:
RewriteEngine On
RewriteRule ^myproject$ http://mydomain.me/myproject/ [NC,L]
RewriteRule ^myproject/(.+)?$ fold1/fold2/yymmdd-myproject/$1 [NC,L]
Two lines per project.

.htaccess redirect all files/folders after www.website.com/

Hi I am currently moving a site to new hosting, previously they had lots of folders/files in the document root which i have moved to another server i.e. www.website.com/foldera/test.zip or just www.website.com/file.mp3 now on hosting.website.com/file.mp3
because these url's are still in circulation i need to redirect all files to the new location so if the old link is clicked it will re-direct correctly.
They have a large amount of files/folders. Please advise on the best way to do this?
Thanks
Just add this to the htaccess file in the document root of your old website:
Redirect 301 / http://hosting.website.com/
Or, if you'd rather use mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?website\.com$ [NC]
RewriteRule ^(.*)$ http://hosting.website.com/$1 [L,R=301]

Redirecting PDF links from another domain using htaccess

We have two domains, let's call them first.com and second.com
We have a directory in second.com called reports, where all our PDFs are located, but we would like to these same PDFs accessible from first.com as well.
Can we redirect let's say first.com/reports/84839049.pdf to second.com/reports/84839049.pdf using htaccess?
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^domain\.com
RewriteRule (.*) http://domain1.com/$1 [R=301, L]
Yes.
redirect /requested/url http://second.com/result/url
http://httpd.apache.org/docs/1.3/mod/mod_alias.html#redirect
You may want to consider using mod_rewrite though, unless you asked for an .htaccess configuration specifically because you have no access to the server configuration and mod_rewrite is disabled or not loaded.
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
http://webdesign.about.com/od/mod_rewrite/qt/site_redirects.htm
You'll need some grasp of regex for mod_rewrite, but it can make configuration of the redirects a lot faster than having to add a redirect for every file on your site(s).

.htaccess redirect doesn't hide url

my .htaccess in the root folder includes the following lines :
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.htm$ http://example.com/?city=$1 [NC]
when I open the address http://example.com/bla.htm, my browser doesn't hide the GET values specified in the .htaccess, it redirects me to ?city=bla. eventhough I'm not using the [R] switch. This has always worked for me before (as I remember, haven't dealt with htaccess in a while). What's wrong here ?
When you redirect to an entire URL, it doesn't do URL rewriting (you can't exactly rewrite URLs on someone else's website).
Assuming both URLs are on the same server, you need to do something like
RewriteRule ^(.*)\.htm$ index.php?city=$1 [NC]
Also, I'd recommend getting into the habit of using the [L] switch whenever you can - it helps avoid bugs when you have a lot of URLs to rewrite.

.htaccess SSL on certain pages

Basically I'm using drupal and can current redirect to an SSL page. But once on that page and continuing navigation all the pages continue over HTTPS. There is a single page I need SSL on and I need to redirect back after you leave that page. Currently I have this:
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^conference https://url/conference/ [R=301,L]
RewriteCond %{SERVER_PORT} =443
RewriteRule !^conference http://url%{REQUEST_URI} [R=301,L]
Thanks
The Secure Pages module can do exactly what you describe, in a highly configurable manner, so adding additional pages in the future can be done w/o editing .htaccess.
Add this rule to return to HTTP:
RewriteCond %{SERVER_PORT} =443
RewriteRule !^join http://www.example.com%{REQUEST_URI} [R=301,L]
After the form data has been POSTed, redirect to an absolute HTTP URL.
Also, note that the form page itself does not need to use SSL; it is enough for the data to be POSTed to a HTTPS URL. This means that you do not need to use mod_rewrite at all for this.