How to redirect subfolder from non-www to www - .htaccess

I have a wordpress and set with http://www.example.com/ and I want to redirect from the non-www to www one.
The follow sample works for http://example.com to http://www.example.com,
but i also want the following redirection which doesn't work in the current implementation.
//subfoler
http://example.com/wp-admin/ to http://www.example.com/wp-admin/
//with querystring
http://example.com/?p=1034 to http://www.example.com/?p=1034
The current goes to 403 Forbidden, do you have any suggestion?
Should i work with .htaccess or 403 error page to perform simple redirection?
Here is my setting in the .htaccess
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
thanks
More information about my site.
I have used cloudflare and set the following value to my site. Does it matter?
Type Name Value
A sample.com XX.xx.xx.xx
A www xx.xx.xx.xx
A * xx.xx.xx.xx

Related

Cant redirect www to non www via htaccess

i tried many way how to redirect www to non www, but nothing work.
This is currently my htaccess:
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
Website is working on non-www url with https. If i type to browser https://www.example.com it end in 404 error.
................................
I assume you've verified that the target URL https://example.com/ is working when entered directly (if not that's a different problem that has nothing to do with redirection).
You might need to specify:
RewriteEngine on
in the .htaccess file before your directives, to activate the rewriting.
Also, the Apache server conf must be configured to have the rewrite_module loaded (check via apache2ctl -D DUMP_MODULES), and to allow its use in your .htaccess file (via the AllowOverride directive).

403 Forbidden when I try to access my website without www

I'm facing a strange problem.
I can access my website with :
https://www.example.com
http://www.example.com
https://example.com
But I can't access to it by tipping :
http://example.com
example.com
I try to put an .htaccess to redirect the non www to the www version but it doesn't work.
# Redirect to www
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Do you know why ?
Thanks.
The problem might come from your DNS settings.
You need to create a record for your domain name without the leading "www.": it should be an A record with the name of “#” that points to your server IP

How to set up 301 redirect to a new domain

I have a domain for which I would like to set up both a www and non-www redirect to https://newdomain.com.
So olddomain.com and www.olddomain.com should go to https://shinynewdomain.com.
#redirect http non-www to https://www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$
RewriteRule (.*) https://www.newdomain.com/$1 [R=301,L]
#redirect https non-www to www
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^olddomain\.com$
RewriteRule (.*) https://newdomain.com/$1 [R=301,L]
This is the code I'm using, and it sends me to a domain parking page at my registrar instead of the new domain at a different host.
Am I doing anything wrong?
There are several ways you can do the redirection of your old website to your new website. If your website is made using wordpress then it will be more easy to achieve 301 redirections.
Method 1:
In Wordpress, what you can do is install the plugin Simple 301 Redirect.
Setup the plugin as you like.
Then you will find options in Settings after activating it.
Method 2
Another is using some code line in the .htaccess file.
In the web directory settings, you will find the access of .htaccess file.
Open the file in the editor and use the code below.
To redirect a single page: Redirect 301 /old-file.html http://www.example.com/new-file.html
To redirect to a new website: Redirect 301 / http://www.new-example.com/

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.

htaccess site ip redirect to www

I already have a few rules set up redirecting users, like none www to www.
but google is still indexing the site ip address i would like this to be redirect to www.and the same if a user tried it
dose any one know how to write this in the htaccess file ??
Redirect any request where the host does not start with www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

Resources