Redirecting subdomain to subdirectory through htaccess - apache

I've been trying to redirect a subdomain to a subdirectory through htacess..
I have a domain lets say sub.domain.com and the directory domain.com/site/
I want to redirect sub.domain.com to domain.com/site not changing any url, simply redirecting in a SEO friendly way.
I've tried a redirect 301 rule but it doesn't seem to have worked.

Try this in your .htaccess:
RedirectMatch 301 wiki.comp.tf(.*) comp.tf/wiki$1
If that does not work, an alternative option is:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^wiki.comp.tf
RewriteRule ^(.*)$ http://comp.tf/wiki$1 [L,NC,QSA]
Some potential options for you using actual names:
Redirect 301 wiki.comp.tf comp.tf/wiki

You can use the following rule :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^(.*)$ /site/$1 [L]
This will internally redirect
sub.domain.com
to
/site/

Related

.htaccess: 301 redirect not working in .htaccess in rewritten root path

I have a domain called example.com which points to /public_html/ on my server.
In /public_html/.htaccess I have the following code to change the root path for my domain example.com to /public_html/example_path/:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteRule !example_path/ /example_path%{REQUEST_URI} [L]
This is working very well.
Now, I want to redirect example.com/test.png to example.com/images/test.png. To do this, I'm using the following code in /public_html/example_path/.htaccess:
RewriteEngine On
Redirect 301 /test.png /images/test.png
Unfortunately, this isn't working, there's no redirection. The redirect is only working if I put the code in /public_html/.htaccess but not in /public_html/example_path/.htaccess.
Why that?
Inside your /public_html/.htaccess have redirect rule before example_path rule:
RewriteEngine On
RewriteRule ^test\.png$ /images/test.png [L,NC,R=301]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule !example_path/ /example_path%{REQUEST_URI} [L,NC]
Note that you cannot have image redirection rule inside /example_path/.htaccess because your image URL doesn't have /example_path/.

301 redirect for individual pages .htaccess

I have tried various ways to write the .htaccess file but all i get is a 500 server error.
I am trying to redirect all the pages from one website to another. This is because the website has been rewritten and has had a change of name. I want to keep all the seo intact.
I have put the .htaccess file in the root directory and here is a copy
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{http_host} ^yachting-school.co.uk [NC]
RewriteRule ^(.*)$ http://www.yachting-school.co.uk/$1 [R=301,L]
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html\ HTTP/
RewriteRule ^(.*)index\.html$ http://www.dreamortwosailing.co.uk/$1 [R=301,L]
#
Redirect 301 /index.htm http://www.dreamortwosailing.co.uk/index.php
Redirect 301 /essential_stuff/about_us.php http://www.dreamortwosailing.co.uk/info/us/
I think your syntax is slightly out on the rules.
Redirect 301 ^/index.htm$ http://www.dreamortwosailing.co.uk/index.php
Redirect 301 ^/essential_stuff/about_us.php$ http://www.dreamortwosailing.co.uk/info/us/

Apache rewrite sub2.domain.com/sub1/sub2 to sub2.domain.com

How to rewrite so that the subfolders go away in the browser URL?
We have a new subdomain that is already in DNS going to a subfolder:
Entering sub2.domain.com changes the url in the browser to sub2.domain.com/sub1/sub2.
RewriteCond %{HTTP_HOST} ^sub2\.domain.com$
RewriteRule ^(/)?$ sub1/sub2 [L]
I thought it's supposed to redirect first then rewrite? But this doesn't work:
RedirectMatch 301 ^/sub1/sub2/.*$ http://sub2.domain.com/
RewriteCond %{HTTP_HOST} ^sub2\.domain.com/sub1/sub2$
RewriteRule ^(.*)$ http://sub2\.domain\.com [L]

Redirecting domain without WWW to the same with WWW

I'm trying to redirect a domain without WWW to the same domain with WWW at the same path with Apache.
So my .htaccess is:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain\.com\.ar$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com.ar/$1 [R=301,L]
But the redirection, for example, by requesting "http://mydomain.com.ar/myimage.jpg" results on this URL: "http://www.mydomain.com.ar/mydomain.com.ar/myimage.jpg".
What am I doing wrong?
EDIT: Sorry, but was my problem, I am pointing domain in cPanel to the subdirectory public_html/mydomain.com.ar (public_html is also accessible), using the same .htaccess but in that subdirectory actually works.
Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.[a-z-]+\.com.ar [NC]
RewriteRule (.*)$ http://www.mydomain.com.ar/$1 [R=301,L]
</IfModule>
I have employed something similar for many years.

Domain .htaccess redirect issue

I'm trying to create some redirects with .htaccess but I never manage to get it fully functional. Maybe someone here can help me.
What I need is:
http://domain.se and http://domain.com to redirect to http://www.domain.com.
I also need http://domain.se/somefolder, http://domain.com/somefolder as well as http://www.domain.se/somefolder to redirect to http://www.domain.com/folder.
I've tried to accomplish this myself but all I end up with is errors about data not being sent.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# folder rewrite
RewriteRule ^somefolder$ folder [L]
# domain redirect
RewriteCond %{HTTP_HOST} =domain.com [OR]
RewriteCond %{HTTP_HOST} =domain.se
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
This is to be placed in .htaccess file in website root folder. If placed elsewhere some tweaking may be required.
First rule will rewrite (internal redirect) requests to /somefolder to /folder. If you need this to be 301 Permanent Redirect, then replace [L] by [R=301,L]
Second rule will do domain redirect job. This rule will ONLY redirect if domain is domain.com or domain.se. If you want to have redirect from ANY domain name (that your webserver is configured can serve) to www.domain.com then replace those 2 RewriteCond lines with this one: RewriteCond %{HTTP_HOST} !=www.domain.com.
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301]
That should meet all of your requirements. All requests that are not www.domain.com will be redirected to that domain, with the request URI intact.