Reputation: 544
Note: Before you mark it duplicate, please read the complete question. I already applied bellow suggestion which didn't work for me.
1 - mod rewrite from .domain to www.domain htaccess
2 - .htaccess redirect from http://www.domain/ to https://domain.com
3 - .htaccess redirect any domain/directory to www.domain/directory
We've got a shopping site which we're hosting on a host (CloudLogin). The main site is built on Joomla CMS and Magento as shopping cart.
The main site URL is www.domain.com and shopping cart URL is domain.com/shop (Without www).
What we need is to show both URLs with (www).
So we need the Magento folder ie domain.com/shop also to be on www URL.
I found some solutions on stack overflow but they didn't work for me as it always show a message browser (Too many redirection or /page isn't redirecting properly)
Note: The .htaccess file is located in (shop) folder.
Bellow is what I tried so far:
1 -
# anything.com to www.anything.com
RewriteCond %{HTTP_HOST} ^[^.]+(\.[^.]+)?$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301,NE]
The above code removes the (shop) directory from URL which turns into error.
ex: domain.com/shop/cat1/prod1 turns into www.domain.com/cat1/prod1
2 -
RewriteCond %{REQUEST_URI} !^/shop/?.*$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
Above code not even apply www on it.
3 -
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Above code redirects from domain.com/shop/cat1/prod1 to www.domain.com/shop with error message (redirected you too many times.) Which seems infinite loop on page.
4 -
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*) http://www.domain.com/shop/$1 [L,R=301]
This does the same thing as 3.
5 -
#RewriteCond ${HTTP_HOST} doamin.com [OR]
#RewriteCond ${HTTP_HOST} www.doamin.com
#RewriteRule ^(.*)$ http://www.doamin.com/shop/$1 [QSA,R=301,L]
Does anyone know how I can always force the correct use of .htaccess for particular URLs?
I've had a look around SO but couldn't find a suitable answer to this.
Upvotes: 1
Views: 248
Reputation: 41219
Try :
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/shop
RewriteCond www.%{HTTP_HOST} ^(?:www\.)?(www\..+)$ [NC]
RewriteRule ^ http://%1/shop%{REQUEST_URI} [NE,L,R]
Upvotes: 1
Reputation: 1600
Did you try the following?
# anything.com to www.anything.com
RewriteCond %{HTTP_HOST} ^[^.]+(\.[^.]+)?$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/shop/$1 [L,R=301,NE]
Since requests originate from the /shop
directory they will not contain that directory in the URL requested.
Upvotes: 0