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]
Related
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]
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.
For a multistore PrestaShop I have, let's say: example.com [main domain] and example.org [extra store].
It's all setup nicely, without www, but when I visit www.example.org, it brings me to example.com.
I would say this helps:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.org [NC]
RewriteRule ^(.*)$ http://example.org/$1 [L,R=301,NC]
Or just for all the domains, I tried this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
But that also doesn't work. I put all of this before the # ~~start~~ Do not remove this-line.
Does anyone have a suggestion on how to approach this? Thanks in advance!
Try adding the following code to the .htaccess file in root directory of PrestaShop installation:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I found the solution from the PrestaShop forum. I can't put links here. I will explain both www to non-www en non-www to www:
1. www to non-www
RewriteEngine On
RewriteCond %{HTTP_HOST} www.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
2. non-www to www
I haven't tried this because this is not setup in my PrestaShop installation. I've seen that people refer to this code many times, so it must work.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
I don't know why Knowband Plugin's code doesn't work. If someone could clarify that, it would be of great educational value.
Case:
www.domain.com redirects to domain.com
www.domain.com/somecategory does not redirect to domain.com/somecategory.
The links on the page are relative, and this is causing problems with google as all www links are now duplicate content. Is there any way to fix this and force a non-www redirect on all WWW pages regardless of if it's root or not ?
htaccess:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1$1 [R=301,L]
i've also tried
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
I have changed your code to:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
And it seems to be working.
Without the slash in the rewrite rule, I was redirected to domain.compage instead of domain.com/page
I have searched the web all over for a good generic .htaccess script for redirecting non-www to www, and currently i'm using this:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
This works fine, but if i go to a subdomain www. will be added. Does anyone has a good working redirect .htaccess script?
Try this :
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^([^\.]+)\.([^\.]+)\.([a-z]{2,4})$
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
#Vince What if the requested url is something like:
http://www.abc.example.com
IMHO, I think with your method it would never be redirected to:
http://www.example.com
How about this?
# Rewrite domain
RewriteCond %{HTTP_HOST} !^www\.([a-z1-9\-]+)\.([a-z]+)$ [NC] [and]
RewriteCond %{HTTP_HOST} ([a-z1-9\-]+)\.([a-z]+)$ [NC]
RewriteRule ^(.*)$ http://www.%1.%2/$1 [R=301,L]
Also, you guys may find these references useful:
https://www.drupal.org/node/93603
http://www.askapache.com/htaccess/modrewrite-tips-tricks.html