What in .htaccess redirect subdomain apache - apache

What I must to do, when I have got DNS to subdomain and when someone go to blog.example.com the website url is the same but the content is from the main page not from blog folder.
What I should to do? What must be in /var/www/blog/ in .htaccess if anything?
Please help!

You can use the following rule :
RewriteEngine On
#if host is blog.domain.com
RewriteCond %{HTTP_HOST} ^blog\.example\.com$
#then serve all requests from the Root folder.
RewriteRule ^(.*)$ /$1 [NC,L]
Replace blog.example.com with your subdomain in the http_host string.

Related

redirect https://domain to https://www.domain in httpd24

I'm trying to redirect URL responses from https://domain.com to https://www.domain.com in the apache24 configuration, I'm using Redhat Server.
I have tried to create this Rewrite Condition but its not working
RewriteCond %{HTTP_HOST} ^(domain\.com)?$
RewriteRule (.*) https://www.domain.com/$1 [R=301,L]
I have fixed the Issue by using Directive IF Condition powered by Apache2.4
I have added this code to my ssl.conf file where it redirected all requests sent to https:// domain .com to https:// www .domain .com . Add this code before the Tag :
<If "%{HTTP_HOST} != 'www.domain.com'">
Redirect "/" "https://www.domain.com/"
</If>
Thank you all for your comments and trying to fix my issue, really its something I appreciate it. Please take a look # for more information about httpd24 directives https://httpd.apache.org/docs/2.4/mod/directives.html
() is a capturing group. You don't want that when you check HTTP_HOST unless you're going to use it as part of the new URL. Also make sure to add the ^ (start of string) and $ (end of string) on your RewriteRule.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301,NC]
You can test your htaccess here http://htaccess.mwl.be/ without having to upload and refresh your site.

htaccess redirect on a domain that already redirects

We have 3 domains in play here. Domain1.com is the main site, domain2.com is the smaller second site, and domain3.com is a vanity URL that redirects from domain1 to a Spanish version of domain2.
What we need to have happen is for domain3.com/subpage to redirect to domain2.com/subpage, however the original redirect of the vanity URL is overriding any attempts we make.
Is this kind of redirect possible?
Here is the original redirect in domain1.com's htaccess file:
RewriteCond %{HTTP:Host} ^www\.domain3\.com$
RewriteRule (.*) http\://www\.domain2\.com/spanish-version [I,R]
These are the attempts at making the needed redirect happen (also in domain1.com's htaccess file):
RewriteCond %{HTTP:Host} ^www\.domain3\.com/subpage$
RewriteRule (.*) http\://www\.domain2\.com/spanish-version/subpage [I,R]
RewriteRule ^domain3\.com/subpage?$ /spanish-version/subpage [NC,R=302,U]
Rules for your domain3 should look like this :
RewriteCond %{HTTP:HOST} ^(www\.)?domain3\.com$ [NC]
RewriteRule ^/?(subpage_name)/?$ http://www.domain2.com/$1 [NC,R,L]
This will redirect
www.domain3.com/subpage
to
www.domain2.com/subpage
(www.)? in HTTP:HOST variable is to make the "www" part optional in url, so in this case
urls with or without "www" would also work.

Redirect traffic to CDN page

So anytime someone visits this page:
http://www.site.com/page/image_(.*).png
They get redirected to the cdn url, which would be
http://cdn.site.com/page/image_(.*).png
How can I do that using htaccess? I only need this for that url.
Using mod_alias, if you have access to vhost config, in your main vhost (for www) add:
RedirectMatch 301 ^/page/image_(.*)\.png http://cdn.site.com/page/image_$1.png
If you don't have access and have to use an .htaccess file, you need mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?site.com [NC]
RewriteRule ^page/image_(.*).png http://cdn.site.com/page/image_$1.png [R=301,L]

Redirecting base url only using htaccess

I'd like to redirect only the base url to an external site.
For instance, I want example.com redirected to anotherdomain.com but I don't want example.com/path to be redirected.
So far, example.com/path redirects to anotherdomain.com/path. :(
EDIT :
First, thank you for the help! example.com now redirects to another.com without affecting the children paths of example.com.
However, ideally, m.example.com won't redirect to another.com. So it's really just example.com redirecting to another.com.
Add this to your .htaccess in your DocumentRoot. I am assuming that you are hosting only one domain on the server.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^m\.
RewriteRule ^$ http://anotherdomain.com [R,L]

Need help configuring 301 permanent redirect in Apache for non www

I am trying to configure my Apache 2.2 version to use a 301 permanent redirect when someone types my url without the www. I want to configure this in the httpd.conf and not using .htaccess if possible. I have tried using Redirect permanent but the first variable has to be a directory and not a url. Any ideas how to configure boom.com requests to be redirected to www.boom.com using a 301 redirect in Apache? Thanks
Add the following:
# Canonical hostnames
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.boom\.com [NC]
RewriteCond %{HTTP_HOST} !=""
RewriteRule ^/(.*) http://www.boom.com/$1 [L,R=301]
This will redirect all request which don't match www.boom.com to www.boom.com, with the same query path. (For example, boom.com/foo?foo=bar will be redirect to www.boom.com/foo?foo=bar).
If you have named virtual hosts you could put the extra RewriteCond entries #tux21b gave inside to isolate them. Also if you have mod_alias you could try this which should do the same thing:
<VirtualHost boom.com:80>
RedirectMatch permanent /.* http://www.boom.com$0
</VirtualHost>
I'm sure someone will comment if there's a reason to use one over the other.