.htaccess rewrite subdomain to directory - apache

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]

Related

Redirect primary domain to sub folder without affecting secondary domain using .htaccess

I have 2 domains:
gyanplease.com/gyanplease primary domain (I have changed the document root with the help of .htaccess)
mobiledevsolutions.com secondary domain
On the document root I have created a folder called gyanplease and with the help of .htaccess I have rewritten gyanplease.com to the gyanplease the folder. This the code for the redirection:
#disable https
RewriteEngine on
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} !gyanplease/
RewriteRule (.*) /gyanplease/$1 [L]
Now when I'm hosting another domain on the same server, that is mobiledevsolutions.com, then it's not working and gives a 404 redirection error.
What you can do is only run the /gyanplease/ rewrite for that host, like this (replacing the last two lines):
RewriteCond %{HTTP_HOST} (?:^|\.)gyanplease.com$
RewriteCond %{REQUEST_URI} !^/gyanplease/
RewriteRule ^(.*)$ /gyanplease/$1 [L]
That way this rule will only affect gyanplease.com.
You can also change line 4 to this, since the capturing is not being used:
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
So all that would make your new rules:
RewriteEngine on
# Disable HTTPS
RewriteCond %{HTTPS} on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Rewrite gyanplease.com to /gyanplease/
RewriteCond %{HTTP_HOST} (?:^|\.)gyanplease.com$
RewriteCond %{REQUEST_URI} !^/gyanplease/
RewriteRule ^(.*)$ /gyanplease/$1 [L]

htaccess Redirect a Specific Directory URLs to another different URL?

With .htaccess how to redirect from:
http://apple.example.com/old_folder
http://apple.example.com/old_folder/
http://apple.example.com/old_folder/bla/bla/bla?a=b&etc
whatever depths under apple.example.com/old_folder --> to:
http://banana.example.com/new_folder/
.. only?
I used following code, but not working:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^apple.example.com/old_folder$
RewriteRule ^(.*)$ http://banana.example.com/new_folder/ [R,L]
You cannot match URI in RewriteCond %{HTTP_HOST} condition.
This rule should work from root .htaccess of apple.example.com:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^apple\.example\.com$ [NC]
RewriteRule ^old_folder(/|$) http://banana.example.com/new_folder/? [R,L]
EDIT: OR else use this rule in /old_folder/.htaccess:
RewriteEngine on
RewriteRule ^ http://banana.example.com/new_folder/? [R,L]

Apache redirect subdomain to folder, keep parameters

I have this code in .htaccess :
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(css|gif|ico|jpg|js|png|swf|txt)$
# If empty subdomain, replace with "www"
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*) http://www.example.com/$1 [QSA,L,R=301]
# If subdomain isn't empty and not "www", redirect to "folder"
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$
RewriteRule (.*) http://www.example.com/%1/$1 [QSA,R=301]
#PAGES REDIRECTION
RewriteRule ^(.*)/register/ /index.php?sub=$1&page=register
RewriteRule ^(.*)/register /index.php?sub=$1&page=register
RewriteRule ^(.*)/lostpass/ /index.php?sub=$1&page=lostpass
RewriteRule ^(.*)/lostpass /index.php?sub=$1&page=lostpass
...
(a rule for wildcard subdmains is already in place and working)
If I browse to http://test.example.com it redirects correctly to http://www.example.com/test but when I try to browse to http://test.example.com/register, it actually redirect to http://www.example.com/test/index.php?sub=http://www.example.com/test&page=register which should redirect to http://www.example.com/test/register
What am I doing wrong here? Thanks in advance!
Try adding the L flag to your second redirect rule, similar to how have it in the first.
RewriteRule (.*) http://www.example.com/%1/$1 [QSA,R=301,L]
It looks like your rewritten URI is passing through to the next rule.
Also, I don't think your first two RewriteCond are in the correct spot.

.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]

How can I use mod_rewrite to 301 redirect example.com to www.example.com?

I need to redirect any URLs without "www." to URLs with "www." for better search engine optimization. I read that this is possible with mod_rewrite and .htaccess files, but I do not know the right code to use. Can anyone help?
Create a file called .htaccess in your root folder (the one where, say, index.html or index.php resides). Put the following into it:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain\.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
There is an excellent example of this in Apache's URL Rewriting Guide.
The following code would redirect any non-www request to a www request:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.example.com/$1 [L,R=301]
You'd want to put this inside the <Directory> directive of your .htaccess file, or apache config file.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) http://www.example.com/$1 [L,R=301]
To remove www from your url website use this code on .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
To force www in your website url use this code on .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^YourSite.com$
RewriteRule ^(.*)$ http://www.yourSite.com/$1 [R=301]
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule ^(([^/]+/)*[^./]+)$ /$1.html [R=301,L]
Were "YourSite.com" you must replace for your url.
In addition to using mob_rewrite you can do this with a virtual host directive
<VirtualHost example.com>
ServerName example.com
Redirect permanent / http://www.example.com
</VirtualHost>
I usually do it the other way around to remove the extraneous 'www'.