how to redirect URL using htaccess - .htaccess

I need to redirect this URL: http://www.africafreak.com/blog/sitemap.xml into http://africafreak.com/blog/sitemap.xml using .htaccess file
Please help me

If your website is hosted on an apache server, a redirect from www to non-www is a simple implementation. Add the following to your .htaccess file.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain\.com
RewriteRule (.*) http://domain.com/$1 [R=301,L]
or try this:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Related

Sub-domain Getting Issue with .htaccess

I have a domain "example.com" and i use following redirection code to redirect it to www in .htaccess file
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
it is working fine until i generate a sub-domain with that domain like "abc.example.com" but it's getting conflicts with htaccess and redirecting the sub-domain to "www.abc.example.com/abc/"
You can use:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|abc)\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

How to redirect only www with htaccess

Can you please help me how to redirect ONLY http://example.com to http://www.example.com?
I have some subdomains (http://sub1.example.com; http://sub2.example.com...) and I don't want them to be redirected also.
This is my .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
With that, what I'm getting is that when I access to http://example.com it redirects correctly to http://www.example.com but when I access to http://sub1.example.com it redirects to http://www.www.sub1.example.com/sub1_folder/sub1_folder/
Thanks in advance
You can have a more generic regex pattern if you know your domain will be XXX.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.com$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Or even better:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[a-z]{2,4}$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Htaccess redirecting non-www to www

I have searched the web all over for a good generic .htaccess script for redirecting non-www to www, and currently i'm using this:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
This works fine, but if i go to a subdomain www. will be added. Does anyone has a good working redirect .htaccess script?
Try this :
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^([^\.]+)\.([^\.]+)\.([a-z]{2,4})$
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
#Vince What if the requested url is something like:
http://www.abc.example.com
IMHO, I think with your method it would never be redirected to:
http://www.example.com
How about this?
# Rewrite domain
RewriteCond %{HTTP_HOST} !^www\.([a-z1-9\-]+)\.([a-z]+)$ [NC] [and]
RewriteCond %{HTTP_HOST} ([a-z1-9\-]+)\.([a-z]+)$ [NC]
RewriteRule ^(.*)$ http://www.%1.%2/$1 [R=301,L]
Also, you guys may find these references useful:
https://www.drupal.org/node/93603
http://www.askapache.com/htaccess/modrewrite-tips-tricks.html

How to redirect using .htaccess file

I'm trying to do something like this...
Redirect www.mysite.com/directory/* to my subdommain.mysite.com/directory/*
Please help me
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteRule ^directory/(.*)$ http://subdommain.mysite.com/directory/$1 [R=301,L]
I want like it
Try adding this to your .htaccess file if you want to redirect all requests from www.mysite.com to subdomain.mysite.com:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteRule ^(.*)$ http://subdomain.mysite.com/$1 [R=301,L]
Try adding this to your .htaccess file if you want to redirect only requests for /anydirectory/ from www.mysite.com/anydirectory/ to subdomain.mysite.com/anyotherdirectory/:
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteRule ^anydirectory/(.*)$ http://subdommain.mysite.com/anyotherdirectory/$1 [R=301,L]

Force www to directory using .htaccess

I have this application directory
http://www.example.com/application
When user browse without www, (e.g. example.com/application), i want to force and redirect them to http://www.example.com/application
How can i achieve using .htaccess.
Thank you.
To redirect non www to www:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
To redirect www to non www, the code is similar:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^my-domain\.com$ [NC]
RewriteRule ^(.*)$ http://my-domain.com/$1 [R=301,L]

Resources