Here's my dilemma now:
I have lots of links from multiple subdomains
https://dev.example.com/abc.html
https://xyz.example.com/abc.html
how can i redirect them using htaccess
Redirect 301 https://dev.example.com/abc.html https://xyz.example.com/abc
obviously doesn't work sine it can only work with /abc
.
And as i mentioned i have a lot of links there, and for some reason i would like to have it in htaccess
Note that the abc is a valid page which exists on the destination url, therefore it will cause infinite loop if i use
/abc
because it will neglect the subdomain.The problem isn't redirecting from subdomain to another, that is not the case, the case is that i have links that i would like to redirect from subdomains to another, specific links.
Therefore i'm not looking for something like nonwww to www kind of syntax.
RewriteEngine on
RewriteBase /
rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]
Unless it can be done with something like grouping all links under a certain subdomain dev.example.com
then have all the links listed under it followed by the target url.
That could be done with
php
, but i prefer to have it inhtaccess
.
The closest solution i believe it could work is to have those links scattered inside apache's vhosts
<VirtualHost *>
ServerName dev.example.com
Redirect 301 / http://example.com/
# Have the rest of links here
</VirtualHost>
Then
<VirtualHost *>
ServerName xyz.example.com
Redirect 301 / http://example.com/
# have the rest of the links here
</VirtualHost>
Valid?
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^tags\/xyz\.html\ $ "https\:\/\/dev\.example\.com\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^abcd$ "https\:\/\/dev\.example\.com\/" [R=301,L]
That's what i got in htaccess
when i create the redirect from cpanel
. so it validates what the solution i reached.
I'm close to finding the solution, now for ease of managing these, i'm looking for a way to group them all in one rule container.
VirtualHost
section is only allowed in Apache config or vhost config files and it requires sudo
or root
privileges to edit those files.
In absence of those privileges, it is always cleaner to have separate directory for each subdomain which allows a separate .htaccess
per subdomain. This will allow you to keep all the rules for a subdomain in one .htaccess
. This eliminates need to have RewriteCond %{HTTP_HOST}
type condition before each rule.
For a single .htaccess, you may use this alternate approach where source is always example.com
and target is a subdomain:
RewriteEngine On
# all other rules except redirect links to subdomains go here
# if current domain is not example.com then ignore all the rules below
RewriteCond %{HTTP_HOST} !^(?:www\.)?example\.com$ [NC]
RewriteRule ^ - [L]
# all the redirects to subdomains go below
RewriteRule ^some-faq-item/?$ to https://faq.example.com/some-faq-item [L,NC,R=301]
RewriteRule ^from1/?$ to https://faq.example.com/to1 [L,NC,R=301]
RewriteRule ^abcd/?$ to https://dev.example.com/ [L,NC,R=301]
brilliant approach!