How to add www to my domain and subdomain url - .htaccess

I want to add www to my all sub domain urls.
For example I want to convert url https://sub.mydomain.com to https://www.sub.mydomain.com. Is this possible with htaccess ?
Currently I get an error when I add www before the sub domain
Error: Requested page cannot be loaded.
Details: locale/www/labels.php not found.

If you have Rewrite_Engine (mod_rewrite) on your server, you can use it following way for redirecting of all visitors from URL without "www." to URL with "www." (you can add these lines in the .htaccess):
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]
But first of all you need to fix this error message (i.e. support showing of right pages on URLs with "www."). To do it you need to configre your server properly. But we don't know anything about configuration of your server, so can't help you. Please extend your question.

Related

htaccess redirect url not working /?index.html to /

I have a client project on search engine marketing with a website https://www.iehsacademy.com/,
The site is 13 years old. before the client was using the URL structure like with HTML like this: https://www.example.com/?index.html (Home Page),
https://www.example.com/?cndc.html,
But last year they completely remake the website URL structure and make it to https://www.example.com/ (Home Page), https://www.exsample.com/about. https://www.example.com/contact.
Now, The old URLs and the new URLs are both working. The client wants and also for SEO purposes we want to block the old URL or redirect them to the new URL. I tried many rewrite conditions on htaccess
RewriteRule ^([a-z]+).html https://www.iehsacademy.com/ [QSA,L]
This one also
RewriteCond %{HTTP_HOST} ^www.iehsacademy.com/?cndc.html$
RewriteRule (.*)$ http://www.iehsacademy.com/$1 [R=301,L]
Not Working anything. The client's developer is also confused. Can anyone help with this or where i am doing wrong?
NB: I took permission from the site owner to share the client's URLs and problems.
You can use the following redirection rule :
RewriteEngine On
#redirect /?index.html to /
RewriteCond %{HTTP_HOST} ^www.iehsacademy.com$ [NC]
RewriteCond %{QUERY_STRING} ^index\.html$
RewriteRule ^$ https://example.com/? [L,R=301]
#redirect /?foobar to /foobar
RewriteCond %{HTTP_HOST} ^www.iehsacademy.com$ [NC]
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^$ https://example.com/%1? [L,R=301]
Make sure to clear your browser caches or use a different browser to test the redirection. And do not forget to replace the destination domain example.com with your domain name.

Redirect loop while looking for uri that does NOT contain specific string

I have an site lets say https://example.com/ and I would like to redirect every url that doesn't begins with /something to https://example.com/something/
I'm using Apache 2.4.29 (hosting) and my .htaccess looks like this.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/something(.*)$ [NC]
RewriteRule ^(.*)$ /something/ [R=302,NC,L]
My problem is that when I'm on homepage (/) or any other page I'm redirected to /something/ which is correct but when I'm on /something/ I'm still beeing redirected to /something/ and it loops until ERR_TOO_MANY_REDIRECTS error shows up.
Here is a link to htaccess tester which shows that this should work and should not redirect me to /something/ when I'm already here but it is not the case on my hosting.
I was following this and this question but without success.
With your shown attempts, please try following set of rules. Please place them on top of htaccess rules file in case you already have existing rules.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/?$
RewriteCond %{THE_REQUEST} !something [NC]
RewriteRule ^ /something? [R=302,NC,L]
You can try this rule with THE_REQUEST variable:
RewriteEngine On
RewriteCond %{THE_REQUEST} !\s/something [NC]
RewriteRule ^ /something/ [R=302,L]
Make sure to test it after completely clearing browser cache.
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of other rewrite directives. Example value of this variable is GET /index.php?id=123 HTTP/1.1

Redirect a domain alias a primary domain page?

We have a domain alias which we would like to redirect to the main primary domain which is working all good and well, but how do we redirect the domain alias to a page on the main website.
I have the following code in the .htaccess added (as below) but it is redirecting to the homepage which also breaks the actual page we want to redirect too.
RewriteCond %{HTTP_HOST} ^(www\.)?domainalias\.co.uk$ [NC]
RewriteCond %{THE_REQUEST} !/pirate-golf [NC]
RewriteRule ^(.*)$ /pirate-golf [L,R=301,NE]
RewriteCond %{HTTP_HOST} ^(www\.)?maindomain\.co.uk$ [NC]
RewriteRule ^pirate-golf$ https://www.domainalias.co.uk [L,R=301,NE]
We want the domain alias to redirect to the main domain page like the following example: www.domainalias.co.uk to redirect to www.maindomain.co.uk/pirate-golf.
Any help on this would be great, thanks.
With your shown samples/attempts, please try following. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www\.domainalias\.co\.uk$ [NC]
RewriteRule ^ http://maindomain.co.uk%{REQUEST_URI} [NE,R=301,L]
Make sure these rules are at the top of your htaccess rules file.

Force "https" and "www" redirect - Wordpress

Force "https" and "www" redirect
I am trying to see if there is a way to redirect url to https://www.example.com
For example, if people are entering the url http://example.com/subfolder, then it should redirect to https://www.example.com/subfolder.
This is a Wordpress website. If I go to Settings => General => I see WordPress Address(URL) and Site Address (URL) are grayed out so I can't enter any url. Both shows as http://example.com
I went to phpMyAdmin=> option table to change the WordPress Address (URL) and Site Address (URL) to https://www.example.com but I still see the http://example.com on my wordpress general setting panel.
I looked at the wp-config file and found the following code that appears to be preventing me from entering custom url.
define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST']);
define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST']);
define('WP_CONTENT_URL', '/wp-content');
define('DOMAIN_CURRENT_SITE', $_SERVER['HTTP_HOST']);
It seems like the WordPress Address(URL) and Site Address(URL) are enforced by the codes here.
My hosting provider recommanded using htaccess to enforce the ssl by putting the code below:
#Force SSL on entire site
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://(YOURDOMAIN)/$1 [R,L]
But this guide doesn't really tell us how to enforce www as well.
I found another guide here .htaccess - how to force "www." in a generic way? that explains how to enforce domain.com to www.domain.com. This one supposedly also check https and implement to the rewrite rule.
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]
Now, someone else is recommanding the following code below, claiming that the first one above will only work if you want all sub-domains forwarded to www.yourdomain.com. I am just confused which one I need to choose?
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I am trying to see which option would be the best solution to implement the force redirect? Or should I get rid of those four define() codes from wp-config file so I can setup redirect with wordpress general setting?

Use htaccess to redirect all traffic from root to specific subdomain page

I have a site that has been up for some time. I had a blog on a subdomain for some time. I have decided to do away with the main site and just support the blog subdomain.
I have a redirect setup for this, but it carries all the extra parameters through to the blog which results in a file not found page appearing. I just want the redirect to go to the index page without parameters.
What I currently have in my .htaccess file is this
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^/?(.*)$ "http\:\/\/blog\.foo\.org\/index\.php" [R=301,L]
When I get a request to
http://www.foo.org/foo/foo/?module=foo
it redirects to
http://blog.foo.org/foo/foo/index.php?module=foo
I want it to redirect to
http://blog.foo.org/index.php
You have to specify the query in the replacement to override the original:
RewriteCond %{HTTP_HOST} !^blog\.example\.org$ [NC]
RewriteRule ^ http://blog.example.org/index.php? [R=301,L]
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} ^(www\.)?foo\.org$ [NC]
RewriteRule ^ http://blog.foo.org/ [R=301,L]

Resources