Landing page redirects - .htaccess

Recently, I did a speed test on my website and discovered that I had too many landing page redirects on the website and it was slowing the site down.
I've seen that I need to update the .htacess file with this code.
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I'm not sure whtether the code is specefic to a certain site or can be used on any.
If I update the .htacess document with the code above will this fix my problem?

Related

Using .htaccess to redirect to https, subfolder and prevent naked domain URLs?

I'm having a heck of a time constructing an .htaccess file to do everything that I need. I have a domain that is only hosting a forum in a /forums subdirectory. I have no landing page, nor do I intend to.
So I want to redirect the root domain to the /forums subfolder, force https and THEN PREVENT naked domains from being allowed.
I've got it all working except users can still manually access/choose a naked domain variant. Some users have been bookmarking the naked domain variant and I want to force them, and all users, back into the www. prefix.
The code below takes http:www.example.com or http:example.com and forces it to https://www.example.com/forums/. The problem is users are still able to manually access https://example.com/forums.
I've tried a bunch of different things to force the forums in the /forums subdirectory to always use a www. prefix, but nothing has worked thus far:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) http://www.example.com/forums [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "https\:\/\/www\.example\.com\/" [R=301,L]
With your shown samples/attempts could you please try following. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine on
##Handle non https all urls here.
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
##Handle non www urls here.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
##Handle no uri link with no www page.
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^/?$ http://www.example.com/forums [R=301,L]
##Handle no uri link with www page.
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^/?$ https://www.example.com/ [R=301,L]

htaccess redirect subfolder to root and force https

I'm trying to redirect all URLs with a subfolder to the root, but also force https and www
So, for example, I want to redirect:
example.com/shop/category/product
www.example.com/shop/category/product
example.com/SHOP/category/product
to
https://www.example.com/category/product
I've tried the following code
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}$1
but this doesn't redirect the subfolder, and if I add the subfolder as a previous rule like
RewriteRule ^shop/(.*)$ /$1 [R=301,NC,L]
It just doesn't work.
One thing to point out - I must also make sure that any other requests to the site (with or without the subfolder) are being redirected to the https www version.
Can anybody point me in the right direction or help me solve this? TIA
You can use this single rule for this redirect at top of your .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{REQUEST_URI} ^/shop/ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(?:shop/)?.*$ https://www.%1/$0 [R=301,L,NC,NE]
# rest of your rules go below
Make sure to clear browser cache or use a new browser for testing.

Magento: New website with redirects from old but still need admin access on old site(s)

We have set up a new Magento2 wesite for a client. They have several old Magento1 websites which need to be redirected to this site so I have set up standard 301 redirects in .htaccess to the new site. So far so good.
Problem is the client still wants admin access to the old magento sites to view orders and sales info etc. Now a simple exception on the .htaccess redirect for '^/admin' does and just ends up redirecting to index.php on the new site. Note that I have left the original magento redirects in place.
Is there anyway round this?
Here is what I have added so far - this is in place before Magento redirects.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^oldsite\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.oldsite\.co\.uk$
RewriteCond %{REQUEST_URI} !^/admin
RewriteRule ^(.*)$ "https\:\/\/www\.newsite\.co\.uk\/$1" [R=301,L]
Ah found out from another site, this works with admin url http://oldsite.co.uk/index.php/admin
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/admin
RewriteCond %{REQUEST_URI} !^admin
RewriteCond %{REQUEST_URI} !^/skin
RewriteCond %{REQUEST_URI} !^/js
RewriteCond %{REQUEST_URI} !^/media
RewriteCond %{REQUEST_URI} !^/index.php/admin
RewriteCond %{REQUEST_URI} !^index.php/admin
RewriteRule (.*) https://www.newsite/$1 [R=301,L]

.htaccess force non-www to www with single page exception

We've successfully forced all pages in the site to use WWW using the following code:
##### Redirect non-www to www -- BEGIN
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/TEST_SITE/$1 [R=301,L]
##### Redirect non-www to www -- END
But our registration page is an exception that has to use a URL that doesn't include the WWW. Two days of testing and research and the only thing I can produce are errors and infinite loops. Does anyone have a suggestion for forcing all pages to WWW except this one page?
http://mysite.com/TEST_SITE/component/users/?view=registration
It seems like it should use a simple redirect before the general WWW redirect, but I can't find a comparable solution in the forums. I'm grateful for any ideas....
Does anyone have a suggestion for forcing all pages to WWW except this one page http://mysite.com/TEST_SITE/component/users/?view=registration Yes you can.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{QUERY_STRING} !view=registration
RewriteRule ^(.*) http://www.%{HTTP_HOST}/TEST_SITE/$1 [R=301,L]
Just add a new condition.
#strip www from registration page
RewriteCond %{HTTP_HOST} ^(www\.)(.+)$
RewriteCond %{QUERY_STRING} ^view=registration$ [NC]
RewriteRule ^TEST_SITE/component/users/$ http://%2%{REQUEST_URI} [L,R=301,NC]
# prevent adding www to registration page
RewriteCond %{QUERY_STRING} ^view=registration$ [NC]
RewriteRule ^TEST_SITE/component/users/$ %{REQUEST_URI} [L,NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/TEST_SITE/$1 [R=301,L]

Rewrite Rule Redirection on .htaccess

I have rewrite rule which works on subdomain other than www. I want to force all URLs to start with www and process all the .html with a php:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^catalog\.html$ http://www.domain.com/static.php?staticpage=$1 [L]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
If you hit http://domain.com/catalog.html, it successfully redirects to http:// www.domain.com/catalog.html (without space of course) given that the file does not exist. Or it will just display http:// www.domain.com/catalog.html and renders the actual .html file (without space) if catalog.html exist.
I am trying to achieve http://www.domain.com/catalog.html without displaying the static.php on the browser regardless if the file exists or not. static.php should process every .html page
Thanks in advance!
The %variables from from the last cond regexp, and you need the condition to succeed so how about:
RewriteCond %{HTTPS}s ^..(s?)
RewriteRule ^ - [E=PROTO:http%1]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ %{ENV:PROTO}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I am assuming you want to display contents of static.php when a .html file is requested for. without the static.php getting displayed on the browser.
When you use, http:// an external redirection takes place resulting in the address getting displayed on the browser.
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule (.+)\.html$ static.php?staticpage=$1 [L]
For HTTPS you can follow what #TerryE has suggested.
But, I fail to understand what pages are you trying to redirect to https

Resources