Forcing www using .htaccess not working as expected - .htaccess

I want my domain, theatrenearyou.org, to automatically redirect to www.theatrenearyou.org. I have a .htaccess file in my site's root directory along with some html files, but when I visit https://theatrenearyou.org it does not redirect to https://www.theatrenearyou.org
I'm using InMotionHosting, and I've made sure my .htaccess file is being read by testing out some redirects such as redirecting /pizza to /about.html. It seems like the .htaccess file is being read, but for some reason no matter what I do I cannot get it to force www.
This is what I currently have in my .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
This should redirect to https://www.theatrenearyou.org but instead the URL remains at https://theatrenearyou.org.

Related

Subdomain redirection w/ c-panel, Wordpress problem

I use GoDaddy c-panel subdomain redirect. It creates a directory
example.com/sub.example.com with htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.sub\.example\.com$
RewriteRule ^/?$ "http\:\/\/www\.example\.com\/My_Target_Wordpress_File/" [R=301,L]
This usually works. Visitors to
sub.example.com
are sent to http://www. example.com/My_Target_Wordpress_File/
But for one subdomain this does not work. Visitors are sent to another file, previously used for redirects. I have deleted and recreated the faulty subdomain.
Where is the problem? How do I make this redirect work?
I tried rewrites in the document root but to no avail.

Stop .htaccess from redirecting root to root/index

I have somehow managed to redirect my site from domain.com/ to domiain.com/index and I was hoping someone could take a look at my .htaccess code to see where I have gone wrong.
Quick background story
I had an old website where all files were html
I then bundled all those files into a folder called old and told all
bots via robots.txt not to crawl it. robots.txt remains in the root
folder
A bunch of .php files were then added to the root folder. Each file has its own rel="canonical" tag with href="http://domain-name.com/file-name" - the homepage rel="canonical" ends with /index
Inside the root folder, I created a .htaccess file that:
Redirected .html files to the correct .php file
Redirect index.php to root (this does not seem to work)
Redirect www to non-www
Remove php file extension
I've tried to re-order the code in case I'm, for example, trying to remove extensions before properly redirecting but nothing seems to work.
I've tried playing around with how I'm redirecting index to index and nothing seems to work.
To see what the redirect path looks like I opened Chromes developer tools and selected network. From there I typed in
http://www.domain-name.com/index.html
And the redirect path looked like this
http://www.domain-name.com/index.html
http://domain-name.com/index.html
http://domain-name.com/index (this is my index.php file)
My htaccess code
301 Redirects Old HTML over to new PHP
Redirect 301 /index.html http://domain-name.com/index
Redirect 301 /about_us.html http://domain-name.com/about
Redirect 301 /services.html http://domain-name.com/services
Redirect index.php to root
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://domain-name.com/$1 [R=301,L]
Redirect www to non-www
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain-name\.com [NC]
RewriteRule ^(.*)$ http://domain-name.com/$1 [L,R=301]
</IfModule>
remove php file extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
I'm not sure what I have done wrong but I'm hoping you can help me understand why the homepage is redirecting to
http://domain-name.com/index
Thank you for your time
Change Redirect 301 /index.html http://domain-name.com/index
to Redirect 301 /index.html http://domain-name.com/
File index.php is your DirectoryIndex so request to http://domain-name.com/ load /index.php
If not, please add to top of your .htaccess file directive: DirectoryIndex index.php

Redirect Multiple URL within folder to other domain

I have some files placed under a particular folder in my old domain:
http://www.olddmain.com/subfolder/example1.html
I want to redirect all files under this folder to a new domain.
Example:
http://www.newdomain.com/subfolder/example1.html
How do I do this without losing Ranking of the pages?
Maybe you need this
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.olddomain\.com$
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^olddomain\.com$
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
from "Redirecting to a different domain" in Redirecting a Web Folder Directory to another Directory in htaccess
In the olddomain.com/subfolder/.htaccess file you just need a single mod_alias Redirect directive to redirect all files to newdomain.com/subfolder/<file>:
Redirect 302 /subfolder http://www.newdomain.com/subfolder
Change the 302 (temporary) to 301 (permanent) only when you are sure it's working OK. (301s are cached hard by the browser, so can make testing problematic.)
However, if olddomain.com and newdomain.com point to the same place then you will need to use mod_rewrite and include a condition that specifically checks for the host being requested, otherwise you'll get a redirect loop. For example, in the same olddomain.com/subfolder/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com [NC]
RewriteRule ^ http://www.newdomain.com%{REQUEST_URI} [R=302,L]
This redirects any request for olddomain.com/subfolder/<anything> to http://www.newdomain.com/subfolder/<anything>.
Again, change 302 to 301 when you are sure it's working OK.

Redirecting in .htaccess non to www

For some odd reason when I visit my site without the "www." it redirects properly. However when I try to visit mywebsite.com/test.php it will redirect to mywebsite.com/public_html/test.php.
For some odd reason it adds "public_html" to the redirect breaking the page
My .htaccess is on the same level as the public_html place. Below is the code im using in the .htaccess file.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mywebsite\.com [NC]
RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [L,R=301,NC]

Why isn't my .htaccess 301 redirect working?

I'm trying to redirect this URL to a different subdomain but am getting a 404.
Need to redirect:
www.dustystrings.com/instrumentbuilding / XYZ
To here:
manufacturing.dustystrings.com/instrumentbuilding / XYZ
I have www.dustystrings.com on one server, and manufacturing.dustystrings.com on another server (necessity).
Basically, I want to redirect all www.dustystrings.com/instrumentbuilding/ queries to manufacturing.dustystrings.com/instrumentbuilding/
What's the right .htacess 301 code to do this? (Apache server)
This should work:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.dustystrings.com[nc]
RewriteRule ^.*/instrumentbuilding/(.*)$ http://manufacturing.dustystrings.com/instrumentbuilding/$1 [r=301,nc]
Redirect to www using htaccess redirect
Create a .htaccess file with the below code, it will ensure that all requests coming in to domain.com will get redirected to www.domain.com
The .htaccess file needs to be placed in the root directory of your old website (i.e the same directory where your index file is placed)
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
Please REPLACE domain.com and www.newdomain.com with your actual domain name.
Note* This .htaccess method of redirection works ONLY on Linux servers having the Apache Mod-Rewrite moduled enabled.

Resources