I found this code to force using www.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]
But i have many domain that pointed to same directory. So i need a version of this code for multiple domains. It must run on any domain, is it possible ?
This should work:
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*) http://www.%{HTTP_HOST}/$1 [R=301]
It will redirect all request without a subdomain to www.domainame.tld.
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.
I have a site hosted on godaddy. uses apache.
I used this code in .htaccess to add www prefix to the domain automatically
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/../$1 [R=301,L]
but instead of 'www.example.com' it goes to 'www.example.com/web'
I just want to convert 'example.com' to 'www.example.com'
If you just want to convert example.com to www.example.com then you just need to use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=302,NC]
You can also lay it out like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]
Make sure you clear your cache before testing this. You will notice I've just the flag R=302. This is a temporary redirect, use this while you're testing. If you're happy with the RewriteRule and everything is working, change these to R=301, which is a permanent redirect.
solved by using this
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*)$ http://www.example.com/$1 [R=301]
RedirectMatch 301 ^/web/$ http://www.example.com/
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
I was using this code to remove www from my domain:
# remove www
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
My problem is that I usually use subdomains and it doesn't work for them. (www.sub.example.com).
How do I rewrite to remove www whatever the domain is and even if it has subdomains?
Try this:
#********** Remove www from address **********
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC]
This is a bit more correct than the other solution.
Edit:
I removed [L] as it isn't applicable here.
Though in most cases [NC] non-case sensitive is recommended.
You could probably use capture groups and combine with rewrites... (Completely untested guess)
# remove www
RewriteCond %{HTTP_HOST} ^www.(.*) [NC]
RewriteRule ^(.*)$ http://$1 [R=301,L]