Pointing Subdomains to subdirectories with .htaccess - wordpress

I know there are thousands of threads about this topic, but despite many hours of searc and try, I'm really unable to find a solution to my case. So I decided to ask for help.
This is my panorama
1) I have a domain www.mydomain.com with a Wordpress installation and Wildcard SSL certificate
As you probably know, Wordpress already writes some rules in .htaccess, these rules:
# 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>
2) now I need to put additional wordpress installations (no WPMU, but different installations) in other subdomains, pointing them to subdirectories that I would like to call "subdomain_NAMEOFTHESUBDOMAIN"
3) as far as I know, to extend the SSL certificate I cannot create subdomains via cPanel, but I have to do it with .htaccess rules
This is where I've been able to get:
1) I've created a wildcard CNAME *.mydomain.com
2) I've created a directory inside my public_html folder, called "subdomain_xxx"
3) I've added these rules (before the wordpress rules) to point the subdomain "xxx.mydomain.com" to the "subdomain_xxx" folder
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^subdomain_xxx/ - [L]
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$
RewriteRule ^(.*)$ subdomain_xxx/$1 [L]
</IfModule>
At first it seems to work:
if I go to mydomain.com I get the corretc "first level" wordpress www.mydomain.com
if I go to ANYTHING.mydomain.com I still get www.mydomain.com
if I go to xxx.mydomain.com I'm correctly pointed to the wordpress installation present in the "subdomain_xxx" directory
but there is a problem:
If I try to duplicate the procedure for any other htaccess-subdomain/subdirectory couple,
for example "subdomain_yyy" and "yyy.mydomain.com", duplicating the previous rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^subdomain_yyy/ - [L]
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$
RewriteRule ^(.*)$ subdomain_yyy/$1 [L]
</IfModule>
I keep being redirected to the first subdomain created: xxx.mydomain.com
To be very clear, this is my full .htaccess content:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^subdomain_xxx/ - [L]
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$
RewriteRule ^(.*)$ subdomain_xxx/$1 [L]
RewriteRule ^subdomain_yyy/ - [L]
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$
RewriteRule ^(.*)$ subdomain_yyy/$1 [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>
Surely there's a lot I do not understand about .htaccess (and regex), so I'm not only looking for a quick solution, but also some other infos to deepen my comprehension of this matter.
I hope someone will find the patience for that.
Thanks in advance

I just found out how to correctly do it
The rules I was using were all wrong.
this is the correct way
RewriteCond %{HTTP_HOST} ^xxx.mydomain.com$ [NC]
RewriteRule ^(.*)$ /subdomain_xxx/$1 [L]
If someone has a comment I will be glad to read it, thanks anyway

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]

How to show image from other domain by htaccess in wordpress

I am new to WordPress and I have uploaded my files into two domains, a main domain and a sub domain. In my sub domain I want to show my main domain images by htaccess. I have used below codes but couldnot get any success.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(files/profile_image/.*)$ http://example.com/$1 [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://dev.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?%{QUERY_STRING} [NE,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
I have used above code but couldn't get any success. Can I get any help?
I know this is old, but posting an alternative simple method here:
RedirectMatch 301 /wp-content/uploads/(.*) https://live-site-to-redirect-from.com/wp-content/uploads/$1
I do this all the time with my localhost sites to grab the images from the online production site. In your sub domain .htaccess file use the following.
<IfModule mod_rewrite.c>
# Attempt to load files from production if
# they're not in our dev version
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule wp-content/uploads/(.*) \
http://example.com/wp-content/uploads/$1 [NC,L]
</IfModule>

redirect WP post from one subdomain to WP page on domain subdirectory using htaccess- file

I got a issue with my htaccess-file. I learned a thing or two when it comes on rewriting through the htaccess-file, but this one is really keeping me awake. Hopefully one of you can help me solve with this matter. The situation is the following.
I want to redirect some specific WP blog posts (permanently) from a subdomain to some specific pages on this WP main domain subdirectory. I'm using a WP Multi subdomain website. Here's what I mean:
sub1.domain.com/postname
needs to be redirected to:
domain.com/sub1/pagename
I can't figure out how to get this done. Here's how my WP htaccess- file that's located on the root of this subdomain normally looks like:
#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
<Files 403.shtml>
order allow,deny
allow from all
</Files>
I hope someone can help me with this matter. It will truly help me. Thanks in forward!
Greets, Marc
Add the following HTTP_HOST rule just before your wordpress rules.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^postname/?$ http://domain.com/pagename [R=301,NC,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
To redirect sub.domain.com to domain.com/subdir, you would use
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/subdir/$1 [R=301,L]

wordpress naked domain vs www

I have a wordpress website somewebsite.com
If I visit it naked like that, it works brilliantly, however if I visit it from www.somewebsite.com I get
Warning: Cannot modify header information - headers already sent by (output started at /home/somewebsite/public_html/index.php:2) in /home/somewebsite/public_html/wp-includes/pluggable.php on line 1121
In the wordpress website general settings it's set to somewebsite.com
and in CPANEL I have the following
an A record with somewebsite.com pointing to the ip address
a CNAME record with www.somewebsite.com pointing to somewebsite.com
Just for kicks I changed the setting in the wordpress general settings to www.somewebsite.com which fixed it for if I was to visit it with the www but broke it for the naked domain, so I switched it back.
my .htaccess looks like
# 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
Any help would be greatly appreciated
Simple is best :-) Redirect non-www to www or www to non-www using .htaccess, so user is going to be redirected before loading any script in php.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
In your example:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Remember to change chmod for .htaccess - it can be overwritten by WordPress in some cases.

.htaccess for redirecting wordpress to new domain

I've been reading docs and stackoverflow questions for the past hour trying to figure this out. I'm not experienced with .htaccess but I do have a programming background.
Here is my current .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} domainA.com/$ [NC]
RewriteRule ^(.*)$ http://domainB.com/$1 [R=301,NC,QSA,NE]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
When I go to http://domainA.com, it directs me to http://domainB.com.. good.
But when I go to http://domainA.com/some_page/, I get redirected to http://domainB.comsome_page/
Why am I losing the /? It seems to be in the rule. Removing the RewriteBase didn't work (not sure why it would, but I'm trying everything).

Resources