Wildcard domain redirect with WordPress - wordpress

I have two domain names setup with sub-domains as follows...
blog.domain.com
www.blog.domain.com
blog.domain.info
www.blog.domain.info
Both domains are pointing to the same location on the same server, a directory containing WordPress. (domain.com/blog)
To keep Google happy, I want everything to redirect to this one domain...
blog.domain.com
Here is what's inside the .htaccess file contained in the WordPress directory...
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
So I went into my cPanel and added a 301 Domain Redirect as follows...
blog.domain.info -> blog.domain.com (with "www" optional and wildcard selected).
cPanel then automatically added the following to the same .htaccess file under the WordPress rewrite rules...
RewriteCond %{HTTP_HOST} ^blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.info$
RewriteRule ^(.*)$ "http\:\/\/blog\.domain\.com$1" [R=301,L]
The problem is that the wildcard portion does not seem to work.
When I go to blog.domain.info, I get redirected to blog.domain.com as expected.
But when I go to blog.domain.info/my-post, I do not get redirected at all.
How can I fix this? I've tried rewrite rules that I know work but all I can think of is that the WordPress rules are interfering.
Once it's fixed, can I move these mod-rewrites to the main .htaccess in the hosting account's www root keeping them separate from the WordPress rules? Edit: Answer- NO, they will not work because they are domains parked in directories off the root www.
Thank-you!

Looks like I simply had to move the new rules above the WordPress section. I also added a new one that is supposed to remove the 'www' from the dot com domain name.
This all seems to be working.
Any comments appreciated.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.com$
RewriteRule ^(.*)$ "http\:\/\/blog\.domain\.com\/$1" [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
EDIT
It should be noted that if you change anything in the htaccess file contained between these Wordpress comments, it might get overwritten by Wordpress at a later time. Moving your custom edits outside AND above this block also works and is immune from any changes to htaccess made by Wordpress itself.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.com$
RewriteRule ^(.*)$ "http\:\/\/blog\.domain\.com\/$1" [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

i own multiple domains for my site MethodShop, including methodshop.com, methodshop.net, etc.. When doing maintenance, i'll divert traffic between the different sites so my users don't have their experience interrupted. below is the htaccess wildcard script i use. it takes whatever URL the user attempts to access and mirrors that link on another domain.
for example,
http://methodshop.NET/games/play/bubblewrap/index.shtml
would redirect to
http://methodshop.COM/games/play/bubblewrap/index.shtml
here's the htaccess script for methodshop.net that rewrites all methodshop.net URLs to methodshop.com. just edit it for your domain.
RewriteEngine on
RewriteRule (.*)$ http://www.methodshop.com\/$1 [R=301,L]

Related

website redirections different from .htaccess configuration

I have web page created in Wordpress v5.4, Theme Twenty Twelve and just installed SSL certificate.
I want to have such redirection structure (redirection codes in rectangles), to optimize for SEO:
but according to Google Chrome Site Inspector I have the follwing one:
So the schema is not fully optimized as for two cases there are dwo cascade redirections whereas should be one.
What is weird, current schema doesn't reflect .htaccess file. My .htaccess file looks as follows:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
So, my understanding of the code is that if url is typed in without https://, script rewrites it into version with https:// an nothing else, so there must be another place where other redirections (e.g. with www -> without www) are being made.
I have checked index.php - no redirections.
I also checked additional lines in .htaccess file don't overwrite those "hidden" ones, so if I simply add them I got multiple redirections error in web browser.
Could you please advise me what else should I do to get desired structure of redirections?
First rule redirect all www to https://domainname.com/, second all http to https://domainname.com/, one hop
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule (.*) https://domainname.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://domainname.com/$1 [R=301,L]
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I’m guessing the redirect from www to non-www is due to the address you’ve set in your settings (WordPress dashboard → Settings → General).
For the redirect you want to achieve, you have to add rewrite conditions for the www. in your .htaccess
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

Force https+www in WordPress and non-WordPress subdirectory

I need to force https and www with .htaccess, and I'm looking for a solution that is not domain-specific, but will apply to any domain and any folder (using server variables).
The setup:
domain.com has WordPress installed
domain.com/folder2 is a physical folder and contains a separate site
The .htaccess file in domain.com currently contains:
# force www+https
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !on$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
This seems to work for domain.com, but not for domain.com/folder2: while https is enforced there, the www redirect doesn't work.
I tried placing a second .htaccess file in domain.com/folder2; that doesn't do anything either.
Questions:
How can I make these rules apply to domain.com/folder2 as well?
I've kept the WordPress block separate because WordPress would probably duplicate it when e.g. permalinks are changed via admin, and it doesn't find this block as-is. Is this correct, or is there a better way to consolidate the two blocks?
Thanks!
In .htaccess in /folder2, add the following :
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)$
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R]
Delete your /folder2/.htaccess if you want to place this in your root dir.

Redirecting whole website by htaccess

I am using a wordpress and trying to redirect my whole website to new domain. I want website structure to remain same that is in this way:
"www.domain.com" redirected to "www.domain.ca"
"www.domain.com/post1" redirected to "www.domain.ca/post1"
As my site is of wordpress so htaccess already has some commands. I haven't changed those commands just added some more by that my site home page starts to redirects but not the whole site.According to my research "RewriteRule ^(.*)$ ..." lets the whole website to redirect but unfortunately it is not. All of my htaccess code is given below. I have replaced my domain names in code.
old domain:www.domain.com
new domain: www.domain.ca
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.domain\.ca/$1" [R=301,L]
Thank You.

htaccess redirect problems, Wordpress and static html site not redirecting properly

I'm having problems with a customer's website. I've created a new Wordpress site for him and now I'm having problems with htaccess redirecting.
Before I installed Wordpress on the server, there was two static basic HTML-sites. One in the public_html root using the main domain and a completely different subsite in a subfolder with a htaccess rewrite rule directing the subdomain into that folder.
Now there is Wordpress messing up the redirects. Wordpress of course made modifications to the original htaccess-file. In addition to that, the customer also had to change servers within the hosting provider to get newer php version so I had to write the htaccess file from scratch.
Here is the current htaccess-file :
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /www/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /www/index.php [L]
</IfModule>
# END WordPress
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?subdomainurl.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subdomainfolder/
RewriteRule ^(.*)$ subdomainfolder/$1 [L]
Wordpress in installed in a folder named www and the url is being rewritten, it works beautifully. But I'm having problems with the subdomain. In the code above I've replaced the subdomain url with subdomainurl.com, the site lies in public_html/subdomainfolder/.
Now, when I type www.subdomainurl.com, the redirect works, the site is shown. But www.subdomainurl.index.html typed doesn't. It has to be written www.subdomainurl.com/subdomainfolder/index.html, so I'm clearly not writing the redirect right, adding the subdomain folder as well. I've tried to mingle with that htaccess but can't get it right. Everytime I try to change something I get a 404 in the main domain's Wordpress site.
At the moment these work:
www.maindomain.com/subdomainfolder/index.html
www.subdomainurl.com/subdomainfolder/index.html
And this doesn't, which I'm trying to do:
www.subdomainurl.com/index.html
I'm not a redirect expert, any help would be much appreciated.
Edit/Addon :
Here is the old code from the old server that redirected the subdomain to the subfolder and rewrote the url, it doesn't work anymore so I ditched it.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?subdomainurl.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subdomainfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdomainfolder/$1
RewriteCond %{HTTP_HOST} ^(www.)?subdomainurl.com$ [NC]
RewriteRule ^(/)?$ subdomainfolder/index.html [L]
I don't see an entry in the htaccess file for just the subdomain and for the main domain.
Currently I'm using this code
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /www/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /www/index.php [L]
</IfModule>
END WordPress
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?subdomainurl.com$ [NC]
RewriteCond %{REQUEST_URI} !subdomainfolder
RewriteRule ^(.*)$ subdomainfolder$1
This loads the whole secondary domain (subdomainurl.com) as www.subdomainurl.com/subdomainfolder and it works like that (www.subdomainurl.com/subdomainfolder/index.html, www.subdomainurl.com/subdomainfolder/work.html etc.), but it still isn't what the customer really wanted. I still need to eliminate the subdomainfolder-part from the url by htaccess, so far I haven't had success. I suppose I haven't got it right on the final row of the code.

How do I combine WordPress permalink code and .htaccess redirect?

I'm trying to combine the following code so that the WordPress permalinks work in the main directory, waringis.com (top code) and a second domain, burrowpress.com, is redirected to the subdirectory 'waringis.com/burrowpress' (bottom code) -
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} burrowpress.com$ [NC]
RewriteCond %{REQUEST_URI} !^/burrowpress/.*$
RewriteRule ^(.*)$ /burrowpress/$1
Right now permalinks are working in WordPress and the redirect works but images have to be direct links to waringis.com/burrowpress/images/ instead of burrowpress.com/images/ - Any help is much appreciated...
You need to swap your code blocks around. The [L] flag on the WordPress rules are stopping execution of the file at that line in their code since your special path would "pass" the WordPress REWRITE_COND statements:
RewriteEngine On
RewriteCond %{HTTP_HOST} burrowpress.com$ [NC]
RewriteCond %{REQUEST_URI} !^/burrowpress/.*$
RewriteRule ^(.*)$ /burrowpress/$1 [L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
You don't need two RewriteEngine On statements, but since WordPress is able to rewrite your .htaccess file (depending on how you have it setup) you might want to leave it. If you are updating your file manually, you can remove the second RewriteEngine on directive.
The important part is that I moved your special rules ahead of wordpress.
I'm not a .htaccess expert but i would say that you don't need to include RewriteEngine On twice that you can put the redirect code in the if statement. So you want to redirect to the subdomain but not have your images be kept in the subdomain is this correct?

Categories

Resources