How to redirect from folder to subdomain? - .htaccess

How to redirect with domain
site.com/image/some_symbols
to subdomain
some_symbols.site.com/
I've tried so
RewriteCond %{HTTP_HOST} ^(www\.|)site\.com$ [NC]
RewriteCond %{REQUEST_URI} ^image/(+*)$
RewriteRule ^(.*)$ http://%1.%{HTTP_HOST}/$ [R=301,L]

The %{REQUEST_URI} starts from the / at the beginning; which is why your pattern was not successful.
Better yet, do not rely on another RewriteCond, keeping it simply as:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?site\.com$
RewriteRule ^image/(.+)$ http://$1.%{HTTP_HOST}/ [R=301,L]

Related

Redirecting to https and subfolder .htaccess

Situation: afwezigheden.eu is the main domain with a few coupled domains (= aliases?) like skatespots.be, inviteme.eu, halftwaalf.be... Now I'm trying to redirect those aliasses to a subfolder, without the users seeing it.
Al those sites are in a subfolder _DOMAINNAME. For example: skatespots.be is in afwezigheden.eu/_skatespots
Also I have an SSL certificate, so I would like to redirect all domains to https too.
I tried several things, the last one is this one, where skatespots seems to work and everything is redirected over https.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/_skatespots/
RewriteCond %{HTTP_HOST} ^(www\.)?skatespots\.
RewriteRule ^(.*)$ /_skatespots/$1 [L]
RewriteCond %{REQUEST_URI} !^/_halftwaalf/
RewriteCond %{HTTP_HOST} ^(www\.)?halftwaalf\.
RewriteRule ^(.*)$ /_halftwaalf/$1 [L]
RewriteCond %{REQUEST_URI} !^/_inviteme/
RewriteCond %{HTTP_HOST} ^(www\.)?inviteme\.
RewriteRule ^(.*)$ /_inviteme/$1 [L]
RewriteCond %{HTTP:X-Forwarded-Proto} !http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
What am i doing wrong?
What if you put the folder structure as this:
afwezigheden.eu/sites/skatespots.eu
afwezigheden.eu/sites/halftwaalf.eu
And then use this in your afwezigheden.eu/.htaccess
RewriteEngine on
RewriteCond %{REQUEST_URI} !sites/
RewriteRule (.*) /sites/%{HTTP_HOST}/$1 [L]

Redirecting subdomain to subdomain folder

I'm having trouble redirecting subdomain.domain.com to subdomain.domain.com/folder.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com
RewriteRule ^(.*)$ /folder/$1 [R=301]
This just keeps redirecting. What am I doing wrong?
You created an endless rewrite loop, since ^(.*)$ also matches /folder/....
You need to add a condition to prevent the endless rewriting:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule ^/?(.*)$ /folder/$1 [R=301]

Rewrite www.domainnamenumberone.com/wordpress/path/ to domaintwo.com/wordpress/path

I have two domain names for the same website, and need to do a correct rewrite, so that whenever someone accesses the first domain name and all subdirectories, with, or without www. they get redirected to the second domain and subdirectories without www.
I managed to set the redirect for the domain name without subdirectories, but for whatever reason, subdirectories are not getting rewritten.
So when I go to domainnamenumberone.com, or www.domainnamenumberone.com, i get redirected to domaintwo.com – however, when I go to domainnamenumberone.com/wordpress/path or www.domainnamenumberone.com/wordpress/path I remain there, and nothing gets rewritten.
Here's what I placed in .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.domainnumberone\.com [NC]
RewriteRule ^(.*)$ http://domaintwo.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^domainnumberone\.com [NC]
RewriteRule ^(.*)$ http://domaintwo.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.domaintwo\.com [NC]
RewriteRule ^(.*)$ http://domaintwo.com/$1 [L,R=301]
Would be grateful for your help!
You need to place this rule as very first rule in DocumentRoot/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domaintwo\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^(?:www\.)?domainnumberone\.com [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(\S*)\s [NC]
RewriteRule ^ http://domaintwo.com/%1 [L,R=302,NE]
Then add this line in each child .htaccess like wordpress/ or wordpress/path/ (wherever .htaccess already exists) below RewriteEngine On line
RewriteOptions Inherit
You can use that:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTP_HOST} ^domainnumberone\.com [NC]
RewriteRule ^(.*)$ http://domaintwo.com/$1 [L,R=301]
But it seems to me that it should also work with yours.....
try a different browser (cache problem)

htaccess redirect to domain without subdomain with no hard coding

i need to redirect all requests of form http://static.name.ext/first/second/etc
to http://www.name.ext if first segment is not "assets"
so keep requests of form http://static.name.ext/assets/etc redirect others
i tried with
RewriteCond %{HTTP_HOST} ^static\. [NC]
RewriteCond %{REQUEST_URI} !/assets [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST} [R=301,L]
but %{HTTP_HOST} returns the subdomain and i don't want that
Try:
RewriteCond %{REQUEST_URI} !/assets [NC]
RewriteCond %{HTTP_HOST} ^static\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%1/ [R=301,L]

How would I redirect two domains using .htaccess

I'm trying to redirect one domain to the root and another to a directory. The problem I'm having is the second domain is overwriting the firsts redirection.
Here is what I have.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^http://example.site.net$ [NC]
RewriteCond %{REQUEST_URI} !^/.*$
RewriteRule ^(.*)$ / [L]
RewriteEngine On
RewriteCond %{HTTP_HOST} !^http://example2.com [NC]
RewriteCond %{HTTP_HOST} !^http://www.example2.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{REQUEST_URI} !^/example2_directory/
RewriteRule ^(.*)$ /example2_directory/$1
That's because 'example2.com' and 'www.example2.com' are not 'example.site.net', like your .htaccess rule states...
If you want those rules to apply to those specific domains, you need to remove the '!' in front of them. Otherwise you need to explain what specific domains need to point to what, not just 'one domain' and 'the other domain'.
Edit: Also, you're not supposed to include the 'http://' for your HTTP_HOST conditions.
I think you might not realize you're negating your comparisons with "!"
Also, you dont' have to turn the engine on twice.
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^http://example.site.net$ [NC]
RewriteCond %{REQUEST_URI} !^/.*$
RewriteRule ^(.*)$ / [L]
RewriteCond %{HTTP_HOST} ^http://example2.com [NC]
RewriteCond %{HTTP_HOST} ^http://www.example2.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{REQUEST_URI} !^/example2_directory/
RewriteRule ^(.*)$ /example2_directory/$1

Resources