htaccess sub domain pointing to a folder but dont want to redirect - apache

I have setup a domain with DNS etc... and wanting to point it to use the /blog folder but when you go to blog.domain.com it just redirects to domains.com/blog - i would like to retain the blog.domain.com url if possible
code im have tried
RewriteCond %{HTTP_HOST} ^blog.domain.com
RewriteRule ^(.*)$ http://domain.com/blog/$1 [L,NC,QSA]
and also tried
RewriteCond %{HTTP_HOST} ^blog\.* [NC]
RewriteRule .* http://domain.com/blog/ [L]
Thanks in advance

This done the trick
RewriteCond %{HTTP_HOST} ^blog.domain.com$
RewriteCond %{REQUEST_URI} !blog/
RewriteRule (.*) /blog/$1 [L]

Related

.htaccess redirect request from specific domain to another domain

I have an apache server hosting a site. I have 2 domains pointing to this server, www.mysite.se and www.mysite.com.
I'm trying to figure out how to in the htaccess file to redirect traffic coming from the www.mysite.se domain to www.mysite.com/se/
I've tried several ways but cannot get it to work. I always just end up on the root of the site, as in www.mysite.com instead of the /se/ path.
This is what I've tried so far:
RewriteEngine On
RewriteCond %{http_host} ^www\.mysite\.se [NC]
RewriteRule ^(.*)$ http://www.mysite.com/se/ [R=301,NC]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?mysite.se$ [NC]
RewriteRule ^(.*)$ /se/$1
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?mysite.se$
RewriteRule ^(/)?$ se [L]
What am I doing wrong?
This seems to have finally done it!
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?mysite.se$
RewriteRule (.*)$ http://www.mysite.com/se/ [R=301,L]

.htaccess rewrite subdomain to directory

Is it possible to use .htaccess to rewrite a sub domain to a directory?
Example:
http://sub.domain.example/
shows the content of
http://domain.example/subdomains/sub/
Try putting this in your .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.domain.example
RewriteRule ^(.*)$ /subdomains/sub/$1 [L,NC,QSA]
For a more general rule (that works with any subdomain, not just sub) replace the last two lines with this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.example
RewriteRule ^(.*)$ subdomains/%1/$1 [L,NC,QSA]
I'm not a mod_rewrite expert and often struggle with it, but I have done this on one of my sites. It might need other flags, etc., depending on your circumstances. I'm using this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$
RewriteCond %{REQUEST_URI} !^/subdomains/subdomain
RewriteRule ^(.*)$ /subdomains/subdomain/$1 [L]
Any other rewrite rules for the rest of the site must go afterwards to prevent them from interfering with your subdomain rewrites.
You can use the following rule in .htaccess to rewrite a subdomain to a subfolder:
RewriteEngine On
# If the host is "sub.domain.example"
RewriteCond %{HTTP_HOST} ^sub.domain.example$ [NC]
# Then rewrite any request to /folder
RewriteRule ^((?!folder).*)$ /folder/$1 [NC,L]
Line-by-line explanation:
RewriteEngine on
The line above tells the server to turn on the engine for rewriting URLs.
RewriteCond %{HTTP_HOST} ^sub.domain.example$ [NC]
This line is a condition for the RewriteRule where we match against the HTTP host using a regex pattern. The condition says that if the host is sub.domain.example then execute the rule.
RewriteRule ^((?!folder).*)$ /folder/$1 [NC,L]
The rule matches http://sub.domain.example/foo and internally redirects it to http://sub.domain.example/folder/foo.
Replace sub.domain.example with your subdomain and folder with name of the folder you want to point your subdomain to.
I had the same problem, and found a detailed explanation in http://www.webmasterworld.com/apache/3163397.htm
My solution (the subdomains contents should be in a folder called sd_subdomain:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} subdomain\.domain\.example
RewriteCond $1 !^sd_
RewriteRule (.*) /sd_subdomain/$1 [L]
This redirects to the same folder to a subdomain:
.httaccess
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.example$ [NC]
RewriteRule ^(.*)$ http://domain\.example/subdomains/%1
Try to putting this .htaccess file in the subdomain folder:
RewriteEngine On
RewriteRule ^(.*)?$ ./subdomains/sub/$1
It redirects to http://example.org/subdomains/sub/, when you only want it to show http://sub.example.org/.
Redirect subdomain directory:
RewriteCond %{HTTP_HOST} ^([^.]+)\.(archive\.example\.com)$ [NC]
RewriteRule ^ http://%2/%1%{REQUEST_URI} [L,R=301]
For any sub domain request, use this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.band\.s\.example
RewriteCond %{HTTP_HOST} ^(.*)\.band\.s\.example
RewriteCond %{REQUEST_URI} !^/([a-zA-Z0-9-z\-]+)
RewriteRule ^(.*)$ /%1/$1 [L]
Just make some folder same as sub domain name you need.
Folder must be exist like this: domain.example/sub for sub.domain.example.
Edit file .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

.htaccess rewrite subdirectory to a different subdirectory on a different domain

I'm trying to configure a redirect for the domain kb3mmy.net. I'd like it to work that if somebody goes to kb3mmy.net/blog it redirects them to christiaanconover.com/category/kb3mmy/. This is the only redirect I want to take place like this, so I can hard code the subdirectories.
Also, there's an existing redirect rule set up on kb3mmy.net that sends any traffic to the root of that domain to a sub page on christiaanconover.com, but any sub page traffic to kb3mmy.net to the corresponding sub page on christiaanconover.com, so that rule cannot be affected by the new one. Here's the code for the existing rule:
RewriteRule ^$ http://christiaanconover.com/kb3mmy/ [L,R=301]
RewriteRule ^(.*)$ http://christiaanconover.com/$1 [R=301]
Any help is appreciated.
RewriteRule ^blog/?(.*)$ http://christiaanconover.com/category/kb3mmy/ [R=301,L]
RewriteRule ^$ http://christiaanconover.com/kb3mmy/ [L,R=301]
RewriteRule ^(.*)$ http://christiaanconover.com/$1 [R=301,L]
How about:
RewriteCond %{HTTP_HOST} kb3mmy.net
RewriteRule ^blog/?(.*)$ http://christiaanconover.com/category/kb3mmy/ [R=301,L]
RewriteCond %{HTTP_HOST} kb3mmy.net
RewriteRule ^$ http://christiaanconover.com/kb3mmy/ [L,R=301]
RewriteCond %{HTTP_HOST} kb3mmy.net
RewriteRule ^(.*)$ http://christiaanconover.com/$1 [L,R=301]

RewriteCond and RewriteRule in .htaccess

I have a client folder located at http://www.example.com/client
However, I've now installed SSL on the server, and want to add a permanent redirect using HTACCESS so that whenever /client is accessed, that it redirects to: https://www.example.com/client
Anybody know how to do that?
I've redirected my domains in the past like this:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
This should not affect the solution, but the site must still redirect to www.example.com FIRST, and then to https://www.example.com/client if for example, http://www.example.co.za/client is entered.
Try this:
RewriteCond %{HTTPS} !on
RewriteRule ^client(/.*)?$ https://www.example.com%{REQUEST_URI} [L,R=301]
RewriteEngine On
RewriteRule ^/?$ https://www.example.com/client [301,NC,L]
It tells the apache, whenever the url is https://www.example.com or either with slash at the end, will redirect to ur /client

.htaccess rewrite and subdomains

I have a subdomain setup as onlinedev.domain.com
I need to use htaccess to rewrite to domain.com/online_content, while still showing onlinedev.domain.com in the address bar (SSL is for onlinedev.domain.com).
this is what I currently have that is very close:
php_flag display_errors off
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} onlinedev\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST}<->%{REQUEST_URI} ^(www\.)?([^.]+).*<->/([^/]+) [NC]
RewriteCond %2<->%3 !^(.*)<->\1$ [NC]
RewriteRule ^(.+) /%2/$1 [L]
This correctly rewrites to domain.com/onlinedev, but if I try to change the RewriteRule to:
RewriteRule ^(.+) /online_content/$1 [L]
I get an error
I understand that there are typically better ways to do this subdomain work, but without getting into server config and DNS details, I need to do it with htaccess.
And yes, I do need to rewrite to a directory that has a different name than the subdomain.
Well, I figured it out.
The issue was that I was causing an infinite loop.
Once the rewrite had happened, it was still trying to rewrite to the directory.
Here is my new htaccess that took care of it:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} onlinedev\\.domain\\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/online_content/
RewriteRule ^(.+) /online_content/$1 [L]
Notice that I added a check to make sure that the REQUEST_URI is not the name of the directory I am rewriting to.
Try this rule:
RewriteCond %{HTTP_HOST} =onlinedev.example.com [NC]
RewriteCond $0 !^online_content/
RewriteRule .+ /online_content/$0 [L]