redirect site to WWW except Api routes - .htaccess

I want to redirect all routes of my site to www.mysite.com
This is my Try:
RewriteCond %{HTTP_HOST} ^xxx.com$ [NC]
RewriteRule (.*) http://www.xxx .com/$1 [R=301,L]
but I want to use API routes without WWW prefix

Change your RewriteRule line to this
RewriteRule ^((?!api).*)$ http://www.example.com/$1 [L,R=301,NC]
The pattern ((?!api).*) means to match any uri but api . This means that http://example.com/api will not get redirected to http://www.example.com/api .

Related

How to redirect some subdomain pages to another page htaccess

I need to redirect a few (not all) subdomain pages to another address in .htaccess:
How can I redirect the following:
spb.example.com/blog/
ekb.example.com/blog/
spb.example.com/projects/
ekb.example.com/projects/
to
example.com/blog/
example.com/projects/
I've tried:
RewriteCond %{REQUEST_URI} ^spb.example.com/blog/$
RewriteRule ^.*$ https://example.com/blog/? [R=301,L]
But it isn't working.
RewriteCond %{REQUEST_URI} ^spb.mytestsite.com/blog/$
RewriteRule ^.*$ https://mytestsite.com/blog/? [R=301,L]
The REQUEST_URI server variable contains the URL-path only. It does not contain the requested hostname (which is present in the HTTP_HOST server variable).
For example:
# Redirect "spb.example.com/blog/" to "example.com/blog/"
RewriteCond %{HTTP_HOST} ^spb\.example\.com [NC]
RewriteRule ^blog/$ https://example.com/blog/ [R=301,L]
However, you can do all 4 redirects in a single rule by using a more flexible regex and backreferences.
For example:
RewriteCond %{HTTP_HOST} ^(?:spb|ekb)\.example\.com [NC]
RewriteRule ^(blog|projects)/$ https://example.com/$1/ [R=301,L]
The $1 backreference contains either blog or projects from the requested URL-path (captured in the RewriteRule pattern).
NB: Test first with 302 (temporary) redirects to avoid potential caching issues.
UPDATE:
How can I redirect all blog articles from subdomains the same rules For example: from spb.example.com/blog/my-blog-article-1/ ekb.example.com/blog/my-blog-article-1/ to example.com/blog/my-blog-article-1/
You could do it like this by adding a second capturing group to the existing rule above:
RewriteCond %{HTTP_HOST} ^(?:spb|ekb)\.example\.com [NC]
RewriteRule ^(blog|projects)/(.*) https://example.com/$1/$2 [R=301,L]

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.

Wild card URL rewrite of subdomain to subdirectory

I am in a situation where an user can create his blog in a subdomain.
Users will create blogs and enter the address he wants like say,
abcd.domain.com Then my php code creates a directory called abcd.
To view the blog user will type in abcd.domain.com in his browser and I want a .htaccess code which will rewrite the url and open the files inside the domain.com/abcd
But for the user the url in the browser should stay abcd.domain.com
Currently I am trying this code
RewriteCond %{HTTP_HOST} ^test\.domain\.com$
RewriteCond %{REQUEST_URI} !^test/
RewriteRule ^(.*)$ /test/$1 [L,QSA]
But this gives me 404 even though I have a file test.html inside the test folder and trying to view that page.
Also in this situation I will have to manually make change to the .htaccess file for URL rewrite. What I want to know is if it is possible to have a wild card subdomain redirect to the respective directory.
You can use:
RewriteCond %{HTTP_HOST} ^test\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule ^(.*)$ /test/$1 [L,QSA]
REQUEST_URI with leading /.
With wild card subdomain:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/%1/
RewriteRule ^(.*)$ /%1/$1 [L,QSA]
Note that it takes more than a rewrite rule to have wildcard subdomains. Just fyi.
You need to have created a wildcard DNS record for subdomains and also tell apache to use any subdomain request by having a ServerAlias of *.domain.com in the apache config.
Then try your rule this way and see if it works for you.
RewriteCond %{HTTP_HOST} ^((?!www).+)\.domain\.com$ [NC]
RewriteCond %1::%{REQUEST_URI} !^(.*?)::/\1/?
RewriteRule ^(.*)$ /%1/$1 [L,QSA]

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.

How do I redirect all subdomain requests to primary domain in htaccess?

I need to redirect all subdomain requests to be redirected to my primary domain in my htaccess. I need it to also include some sort of wildcard redirect.
e.g. washington.mysite.com/ redirect to mysite.com/washington
but I need to to also redirect any old url on the subdomain to mysite.com/washington
e.g. washington.mysite.com/category.aspx?washington-attractions&CatID=17 redirect to my-site.com/washington
This is my code so far:
RewriteCond %{HTTP_HOST} ^washington\.mysite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.washington\.mysite\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.mysite\.com\/washington/$1" [R=301,L]
However it still appends the old URL to the new one
e.g. washington.mysite.com/category.aspx?washington-attractions&CatID=17 redirects to my-site.com/washington/category.aspx?washington-attractions&CatID=17
Basically I need the redirect washington.mysite.com/*(anything) to my-site.com/washington
Any suggestions would be much appreciated :)
RewriteCond %{HTTP_HOST} ^(www\.)?washington\. [NC]
RewriteRule ^(.*)$ http://www.mysite.com/washington [R=301,L]
In your RewriteRule you have /$1 which matches the (.*) wildcard set of parentheses. This is why you're getting the path from the old URLs appended.
I combined your 2 RewriteCondition's, making the (www\.) match optional with ?.
The [NC] flag is the nocase flag.
To clear the querystring, append ? to the end of the rewrite URL
RewriteCond %{HTTP_HOST} ^(www\.)?washington\. [NC]
RewriteRule ^(.*)$ http://www.mysite.com/washington? [R=301,L]

Resources