Basically I want sub.domain.com to redirect to domain.com, but only that url. For example sub.domain.com/page should still load without redirecting. How can I do this .htaccess?
Your .htaccess should look like
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule .* http://domain.com [R,L]
This should work within .htaccess placed in the root directory. The empty rule (nothing between ^$) is interpreted as / (the root). Your subpages and subdirectories should remain unaffected. Requires mod_rewrite.
RewriteEngine on
RewriteRule ^$ http://new.domain.com/ [L]
# subdomain home page to main domain page
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule .* https://www.example.com [L,R=301]
This works for me as I wanted to have 301 redirect and to https main domain page. I hope this helps. Thanks..
Related
I have looked at a few of the other .htaccess questions, but none have my specific usecase.
SITUATION
I have a website, say example.com. It has a sub-domain sub.example.com. The web host also provides a folder for every sub-domain: example.com/sub
I want to link both the sub.example.com and example.com/sub to othersite.com/some/folder/path/
What I have so far (the code also adds www things, don't ask why, unless it can fix things):
in /.htaccess:
RewriteBase /
##Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com[nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]
##301 Redirect Entire Directory
RedirectMatch 301 /sub/(.*) https://othersite.com/some/folder/path/$1
in /sub/.htaccess:
RewriteBase /sub/
##Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.example.com[nc]
RewriteRule ^(.*)$ http://www.sub.example.com/$1 [r=301,nc]
##301 Redirect Entire Directory
RedirectMatch 301 /(.*) https://othersite.com/some/folder/path/$1
THE PROBLEM
When I use sub.example.com/foo/bar the redirect works (it redirects to https://othersite.com/some/folder/path/foo/bar)
When I use example.com/sub it breaks (it redirects to https://othersite.com/some/folder/path/sub/foo/bar, notice the ../sub/..)
What do I need to change to make it work so that the sub folder isn't added to the path?
Don't mix mod_alias rules in mod_rewrite. Keep your /sub/.htaccess as
##Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteBase /sub/
RewriteCond %{HTTP_HOST} ^sub.example.com[nc]
RewriteRule ^(.*)$ http://www.sub.example.com/$1 [r=301,nc]
##301 Redirect Entire Directory
RewriteRule (.*) https://othersite.com/some/folder/path/$1 [L,R=301]
Test this after clearing browser cache.
This sounds like something that mod_dir does because you're accessing a directory without the trailing slash. By default accessing a directory without a trailing slash gets the request flagged for a redirect to the same directory with the trailing slash. This often messes rewrites or other redirects. Try turning it off and adding an additional redirect to handle the trailing slash:
RewriteBase /
DirectorySlash Off
##Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com[nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*[^/])$ /$1/ [L,R]
##301 Redirect deviantart Directory
RewriteRule ^sub/(.*) https://othersite.com/some/folder/path/$1 [L,R=301]
I moved my site from blog.abc.com to www.abc.com
I want to redirect every request made to blog.abc.com to abc.com
For example:
If blog.abc.com/example.html is requested, it should redirect to www.abc.com/example.html, how can I do it via .htaccess only?
You can put this code in your htaccess (in blog.abc.com root folder)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.(.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
Using mod_rewrite you could do it like this:
RewriteEngine On
RewriteCondition %{HTTP_HOST} ^blog.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R,L]
or if blog.example.com has its own vhost you could also use mod_alias:
Redirect / http://example.com/
I use 301 redirect from non-WWW to WWW domain! Works fine, but links are different.
Example:
from - ggkj.com/posts/godzilla/43
to - www.ggkj.com/index.php?view=posts&author=godzilla&aid=43
my .htaccess 301 redirect code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*) http://www.example\.com/$1 [L,R=301]
.htaccess code: http://pastebin.com/t3FA59uq
This is currently what i use on my website with success:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{http_host} !^www.walterbax.ca [NC]
RewriteRule ^(.*)$ http://www.walterbax.ca/$1 [R=301,L]
EDIT: after looking into your question a little further I can tell you that your issue is improperly placed in the file. it must be hitting another rule that is rewriting it into URL variables.
I would start with a basic htaccess for the www. rule for testing and add to it from there.
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain.com$
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]
I have domain like:
someword.com.s123234.gridserver.com
I am trying to .htaccess redirect like
RewriteEngine on
RewriteCond %{HTTP_HOST} !^someword.com.s123234.gridserver.com/$
RewriteRule ^/(.*)$ http://www.someword.com/$1 [R=301,L]
but the redirect is not working.
How can I permanently redirect this type of domain?
Thank you
Since the point means something to htaccess (namely "all characters), you have to write . instead.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^someword\.com\.s123234\.gridserver\.com$
RewriteRule ^/(.*)$ http://www.someword.com/$1 [R=301,L]
Im migrating my website from the .co.uk to the .com but need to setup a 301 redirect so all of the individual pages will still be routed properly.
ie i want http://www.mydomain.co.uk/shopping/product1 to go to http://www.mydomain.com/shopping/product1
I have done this before but for the life of me cannot remember how.
many thanks
paul
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
This redirects both the www and non-www for mydomain.co.uk to www.mydomain.com.
To redirect any (sub-)domain other than mydomain.com, use
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^mydomain.com$ [NC]
RewriteRule ^(.*)$ http://mydomain.com/$1 [L,R=301]
Might be useful if you have other domains pointing to the same website.
It also redirects the www.mydomain.com to mydomain.com.
This is accomplished using a simple rewrite placed in your .htaccess file.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain.co.uk$ [NC]
RewriteRule ^(.*)$ http://mydomain.com/$1 [L,R=301]