Conditional htaccess redirect - .htaccess

Is it possible to make a conditional redirect using .htaccess in a way that if somebody access my domain directly (example.com) he will be redirected to a subdomain (subdomain.example.com) or may be to another domain, but if someone accesses it through a particular URL (example.com/magic) he get's redirected to example.com i.e. main domain.
In other words, I wanted my users to access the main domain (example.com) only if they are using example.com/magic.
My htaccess is presently redirecting all users of main domain to a subdomain.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "https\:\/\/sub\.example\.com" [R=301, L]
I couldn't find though, how to put the condition in my htaccess.

You may use this rule to redirect every URI that is not /magic or /magic/ to a sub-domain:
RewriteEngine On
# redirect /magic
RewriteRule ^magic/?$ / [L,NC,R=301]
# redirect anything except /magic
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule !^(magic/?)?$ https://sub.example.com [R=301,L,NE]

Related

Redirect Specific Wildcard Subdomain to a specific url of another domain

I want to redirect specific wildcard subdomain to a specific url of another domain. the both domains are on the same server/public_html, and redirect all the inner pages of the wildcard subdomain to the inner pages of the other domain.
e.g
redirect music.example.com to stackoverflow.com/music/
and
redirect music.example.com/happy-birthday/ to stackoverflow.com/happy-birthday
redirect music.example.com/sing-to-you/ to stackexchange.com/sing-to-you/
I have more than 100k post so i cannot make the redirection of the inner pages one by one using .htaccess
Please help me out,
I tried the below code
RewriteCond %{HTTP_HOST} ^(music.example.com) [NC]
RewriteRule ^()$ https://www.stackoverflow.com/music/ [R=301,L]
but it only redirect the homepage to the specific url which I wanted, but the rest of the urls is the main issue, If I use the normal domain redirection, it works for the rest of the urls, but the function of the code above will stop working
Here is the below code for normal domain redirect redirection
RewriteCond %{HTTP_HOST} ^music\.domain.com\.com [NC]
RewriteRule ^(.*)$ https://wwww.stackoverflow.com/$1 [L,R=301,NC]
I used the below htaccess code and it works perfectly.
RewriteEngine On
# "music.example.com/" to "another.example/music/"
RewriteCond %{HTTP_HOST} ^music\.example\.com [NC]
RewriteRule ^$ https://another.example/music/ [R=302,L]
# "music.example.com/<something>" to "another.example/<something>"
RewriteCond %{HTTP_HOST} ^music\.example\.com [NC]
RewriteRule (.+) https://another.example/$1 [R=302,L]
The RewriteCond (condition) directive checks the requested host. The RewriteRule naturally redirects to the other domain. In the second rule the URL-path from the request is captured in the $1 backreference.

301 redirect to new domain with same url structure

I have a Magento website that is moving to a new domain. Im looking to 301 redirect all pages from the old domain to the new domain keeping same url structure.
I've updated the .htaccess file on my old domain with:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^new-domain.com$ [NC]
RewriteRule ^(.*)$ http://new-domain.com/$1 [R=301,L]
The issue: The above seems to redirect every instance of: old-domain.com/subdir/whatever only just to the main domain: new-domain.com.
I'd be looking to redirect old-domain.com/subdir/whatever to: new-domain.com/subdir/whatever.
Any idea on what could be wrong?
Adjust your rule to use REQUEST_URI variable which is not dependent on the directory of .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.com$ [NC]
RewriteRule ^ http://new-domain.com%{REQUEST_URI} [R=301,L,NE]
Better to clear your browser cache before testing this rule.
PS: You need to match hostname condition to old-host not the new-host.
This should redirect and retain the path after the main domain.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^old-domain\.com$ [NC]
RewriteRule ^(.*)$ http://new-domain.com/$1 [R=301,L]

htaccess non www to www and remove subdomain www

So I've got the following rewrite code in my htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
//
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.([^\.]*)\.domain\.com$ [NC]
RewriteRule (.*) http://%1.domain.com$1 [R=301,L]
Works perfect for redirecting non-www to www for my domains.
I've got a subdomain, lets call it 'sub.domain.com' which works find. If I goto www.sub.domain.com, it redirecting to 'sub.domain.com/sub/'
Anyone having a idea why?
None of the rules you have in your question routes requests to a subdomain's folder. The /sub/ should never be there if it wasn't originally in the request.
That said, all of your redirect rules need to come before any routing rules. Routing rules being stuff that internally routes requests to other URI's, for example:
RewriteCond %{HTTP_HOST} ([^\.]*)\.domain\.com$ [NC]
RewriteRule (.*) /%1/$1 [L]
That rule internall routes to a folder named the same thing as the subdomain. If this rule were to be before the redirect, both rules get applied and the redirected URI becomes /sub/. You need your routing rules placed after your redirect rules, e.g. all rules that have a http:// or an R flag.

How to .htaccess redirect domain to subdomain while keeping the main domain intact

I want to redirect all urls under my domain
domain.com/urls, www.domain.com/profile/user1.html etc. to subdomain.domain.com/urls and subdomain.domain.com/profile/user1.html etc.
but I dont want to redirect domain.com and www.domain.com to subdomain.domain.com
Is it possible via htaccess?
Edit: I want to redirect just the internal pages and files only. But leaving the main domain.com intact.
More Examples
domain.com/page1.html to subdomain.domain.com/page1.html
www.domain.com/members/admin.html to subdomain.domain.com/members/admin.html
www.domain.com to www.domain.com (no redirection here)
Try:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/.+
RewriteCond %{REQUEST_URI} !^/index\.(php|htm)
RewriteCond %{HTTP_HOST} !^subdomain.domain.com$ [NC]
RewriteRule ^ http://subdomain.domain.com%{REQUEST_URI} [L,R]
If you want to permanently redirect, change the square brackets to: [L,R=301].
You can first handle the main page by not rewriting and redirect everything else with a second rule
RewriteRule ^$ - [L]
RewriteCond %{HTTP_HOST} !^subdomain.domain.com$
RewriteRule ^.+$ http://subdomain.domain.com/$0 [R,L]
When everything works as you expect, you can change R to R=301.

How can I use .htaccess to stop redirecting from subdomains?

I have a domain, "domain.com", and subdomain, "sample.domain.com", and all files related to both are stored in domain.com/folder. How can I use .htaccess to prevent requests from "sample.domain.com" from going to domain.com/folder/index.php?title=sample?
This is what I'm currently using in my .htaccess file:
RewriteEngine on
RewriteCond %{http_host} .
RewriteCond %{http_host} !^www.domain.com [NC]
RewriteCond %{http_host} ^([^.]+)\.domain.com [NC]
RewriteRule ^(.*) http://www.domain.com /folder/index.php?title=%1 [R=301,L,QSA]
But there is one thing - a redirect is external (the browser goes to a new link), and I need to redirect this was on the server side, so that the user thought he was actually working with the subdomain. Can can I accomplish this?
Change this line:
RewriteRule ^(.*) http://www.domain.com /folder/index.php?title=%1 [R=301,L,QSA]
To
RewriteRule ^(.*) /folder/index.php?title=%1 [L,QSA]
The R flag tells the rewrite engine to redirect, and having the http://www.domain.com in the rule's target is also an implicit redirect.

Resources