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.
Related
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.
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]
I'm having a Magento multistore setup and i'm sending all non www requests to www in the .htacces with the rewrite:
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
I'm using a sub domain for testing new websites and for this sub domain I don't need this rewrite. Therefore I use:
RewriteCond %{HTTP_HOST} ^sub\.domain\.com [NC]
RewriteRule ^(.*) - [L]
Only problem now is that when I go to http:// sub.domain.com it works, but when I go to a category or product for example http:// sub.domain.com/cat1 or http:// sub.domain.com/product-red.html it doesn't work any more and I get a "Not Found" message.
What do I need to add to the code so it excludes the whole sub domain, including everything after the / ?
Don't use a separate rule to ignore all requests for subdomain like that otherwise Magento's routing rule will also be skipped. Instead tweak your www rule like this to ignore subdomain:
RewriteCond %{HTTP_HOST} !^(www|sub)\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}/$1 [R=301,L,NE]
My website (https://www.went.digital) have a wildcard subdomain (but sadly not a wildcard SSL), which redirects to a subfolder. That's mean that if you enter to demo.went.digital, you'll see what's in went.digital/subdomain. It doesn't really matter how does it work, but I'm saying it because the .htaccess file affecting the subdomains too.
I add into the .htaccess file code that automatically adds www if it's not a subdomain, and I add to it https:// but it doesn't add the https:// if you entered using www.
Here is my current code in the .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]
You'll have to add a separate rule for that:
RewriteCond %{HTTP_HOST} ^www\.went\.digital$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.went.digital/$1 [L,R=301]
I have a situation where we've redesigned a site and have moved it to a new domain. The old site has a few client subdomains that we need to redirect to the new domain, but to folders that we will explicitly specify. Note that the new domain is actually a subdomain of the old domain, so not sure how that complicates anything, if at all.
For example, oldsite.com currently redirects all request to new.oldsite.com with the following rule:
RewriteCond %{HTTP_HOST} !^\.oldsite\.com
RewriteRule ^(.*)$ http://new.oldsite.com/$1 [R=302,L]
I also want to redirect some specific subdomains to folders of new.oldtsite.com, like subdomain.oldsite.com to new.oldsite.com/subdomain/. There also the likelihood that the old subdomain and the new directory will have different names (subdomain.oldsite.com to new.oldsite.com/subdomain-name/. :-/
I've attempted to duplicate this rule for the subdomain to folder redirect, but that just hijacks all redirects.
I will give you a virtual high-five if you can spot me a fix.
Have your rules from specific to generic. Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^subdomain\.oldsite\.com$ [NC]
RewriteRule ^(.*)$ http://new.oldsite.com/subdomain-name/$1 [R=302,L]
RewriteCond %{HTTP_HOST} ^oldsite\.com$ [NC]
RewriteRule ^(.*)$ http://new.oldsite.com/$1 [R=302,L]
EDIT: To redirect any.oldsite.com/foo to new.oldsite.com/any/foo
RewriteCond %{HTTP_HOST} ^([^.]+)\.oldsite\.com$ [NC]
RewriteRule ^(.*)$ http://new.oldsite.com/%1/$1 [R=302,L]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.