.htaccess wordpress - rewrite subdomains to main domain - wordpress

I have a wordpress site with the wordpress files in a different folder (wpaw)
Wordpress url https://example.com/wapw
Site url https://example.com
I also have 3 or 4 subdomains, for example new.example.com
Currently the main domain and all the subdomains work correctly with the following RewriteRule
RewriteEngine On
RewriteRule %{REQUEST_URI} !^/wpaw/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /wpaw/$1
RewriteRule ^(/)?$ /wpaw/index.php [L]
How can I modify the above so that http(s)://anysubdomain.example.com also redirects to https://example.com

Please write the below code in .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteEngine On
RewriteCond %{HTTP_HOST} ^anysubdomain\.example\.com [NC]
RewriteRule ^(.*) https:/.example.com/$1 [L,R=301]
</IfModule>
Thanks

Related

Redirect wordpress subdomain fails with /admin

I have a wordpress installed in my public_html under the foo.com domain. I've installed a new wordpress site under public_html/bar under the bar.com domain. I have the following htaccess files:
public_html:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?bar.com$
RewriteCond %{REQUEST_URI} !^/bar/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /bar/$1
RewriteCond %{HTTP_HOST} ^(www.)?bar.com$
RewriteRule ^(/)?$ bar/index.php [L]
</IfModule>
When I tried to enter to bar.com, the site displays correctly. But when I try to enter bar.com/admin the site redirects me to foo.com/wp-login. Is there any additional config tha I'm missing?

Homepage doesn't redirect from HTTP to the HTTPS same page

Here's my website structure :
/public_html
/subdir
/wordpress website
My main domain was on public_html and I move the whole to the subdir with this htacess in the root (public_html) (from Wordpress Codex):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/subdir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdir/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ subdir/index.php [L]
</IfModule>
When I access to example.com or www.example.com (without https), it redirects to : https://example.com/subdir
When I access to example.com/page1, it redirects well to https://example.com/page1
It's a bit annoying. How can I resolve this please ?
Thanks
Finally it's ok
This was due to the SSL redirect code in the public_html/subdir/.htaccess files:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I moved this code to public_html/.htaccess and the site is now working as expected.

Wordpress redirection loop error

I created a website few days back with a url http://abctest.com in wordpress. Now the website has been published to a domain http://www.bcdefg.com.
The server is same, I have not migrated the site, only the domain has been pointed to the same server. I have changed the wphome and wpsite url to respective domain.
What I want to achieve is to redirect all http://abctest.com or http://www.abctest.com requests to http://www.bcdefg.com.
I tried to use .htaccess redirection but that leads to Too many redirection error. Any solution ?
Below is my htaccess code.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !bcdefg.com$ [NC]
RewriteRule ^(.*)$ http://www.bcdefg.com/$1 [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Error is on your rewriteCond : your host is www.bcdef.com and not bcdef.com (www is important)
try replacing this
RewriteCond %{HTTP_HOST} !bcdefg.com$ [NC]
by this :
RewriteCond %{HTTP_HOST} !www.bcdefg.com$ [NC]

wildcard ssl .htaccess file causes 500 error on laravel subdomain

I have a wildcard ssl installed on my account (My host is Bluehost) and I followed the directions here from Bluehost's site to install the wildcard SSL. However, when I try and access the site at all it comes up with a 500 internal server error. I am using laravel framework right now for the subdomain, I don't know if that changes anything, but here is the .htaccess file for the main site (Which is a wordpress site):
# Custom subdomain .htaccess SSL + WordPress
RewriteCond %{HTTP_HOST} ^admin.mysite.com$
RewriteCond %{REQUEST_URI} !^/admin/public/
RewriteRule ^(.*)$ /admin/public/$1
RewriteCond %{HTTP_HOST} ^admin.mysite.com$
RewriteRule ^(/)?$ admin/public/index.php [L]
# End custom subdomain .htaccess
# BEGIN WordPress
AddHandler application/x-httpd-php70s .php
<IfModule mod_rewrite.c>
# Custom maindomain .htaccess WordPress
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteRule ^index\.php$ - [L]
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# End custom maindomain .htaccess
</IfModule>
# END WordPress
And here is the .htaccess file for the subdomain (the one with laravel installed):
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
Keep in mind too, I know nothing about .htaccess or how it works, I just did what the webpage said to do.
I suggest you to try below code for simple redirection HTTP to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This should work for your main domain and its sub-domain both. Now if you want to redirect Non-WWW domain to WWW then try below code;
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.domainname.com%{REQUEST_URI} [L,R=301]

Virtual subdomains using .htaccess

I am trying to create virtual subdomains using .htaccess and wordpress.
But it won't seem to work properly. Using wordpress I get urls like:
foo.eu/test-pagina/?lang=fr
I managed to rewrite this to what i want:
fr.foo.eu/test-pagina/
But when this url is rewritten the page returns an 500 internal server error.
I also made a wildcard for the subdomains in my dns configuration.
Do I have to make subfolders for each subdomain in my root ?
My current .htaccess code is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^([a-z]{2})\.hypnose\.eu$ [NC]
RewriteCond %{THE_REQUEST} \ /+([^\?]*)\?lang=([a-z]{2})&?([^\ ]*)
#RewriteRule ^ http://%2.hypnose.eu/%1?%3 [L,R]
RewriteCond %{QUERY_STRING} !(^|&)lang=
RewriteCond %{HTTP_HOST} ^([a-z]{2})\.hypnose\.eu$ [NC]
RewriteRule ^(.*)$ /$1?lang=%1 [L,QSA]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Can anyone tell me what did I do wrong?
Have you added "fr" as your subdomain in the domain's control panel?
Add this in your htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ http://domain.com/test-pagina/%1/$1 [L,NC,QSA]

Resources