Proper way to 301 redirect with htaccess - apache

I have a site of mine that has its own homepage and a blog under domain.com/blog/. What is the proper way of me sending the domain.com/blog/ requests to domain.com/ while sending a 301 to the browser so that search engines know that the URL has moved?
This is what I have, but not working at all.
RewriteEngine on
RewriteCond %{http_host} ^domain.com/blog/ [nc]
RewriteRule ^/blog/$ http://www.domain.com/$1 [r=301,nc]
I have replaced the domain.com to my actual domain.
Thanks in advance!

I don't think you need $1 in RewriteRule since you want to redirect users coming from domain.com/blog to domain.com. Besides, your $1 doesn't substitute anything because you don't use any parentheses in your regex.
EDIT:
This should work for you.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain\.com/blog/$ [NC]
RewriteRule ^/blog/$ http://www.domain.com [R=301,L]

You forgot to escape the periods in the first line.
==NEW CODE==
RewriteEngine on
RewriteCond %{http_host} ^www\.domain\.com/blog/ [nc]
RewriteRule ^/blog/$ http://www.domain.com/$1 [r=301,nc]

Related

apache Rewrite rule appends /html directory for no reason

I have a domain foo.tech.
I want to use a new domain footech.io instead.
The redirect also has to make sure all the URLs work.
E.g foo.tech/bar goes to footech.io/bar
Here is my .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo.tech [NC]
RewriteRule ^(.*) http://footech.io/$1 [R=301,L]
For some reason, it decides to add /html at the end of my domain.
So now if I visit foo.tech it will redirect to footech.io/html
If I visit foo.tech/bar it will redirect to footech.io/html/bar
Please help.
Update:
I think the /html comes from the $1
I've tried to make the rewrite rule as follows:
RewriteRule ^(.*) http://footech.io/$1/$1 [R=301,L]
going to foo.tech leads to footech.io/html//html/
going to foo.tech/bar leads to footech.io/html/bar/html/bar
final update:
I made it work now using this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo.tech [NC]
RewriteRule ^html/(.*) http://footech.io/$1 [R=301,L]
This seems to fix it
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo.tech [NC]
RewriteRule ^html/(.*) http://footech.io/$1 [R=301,L]

Redirecting a domain with htaccess with a few specific 301s

Is it possible to do something like this to redirect old traffic to the new site with a 301.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !new-example.com$ [NC]
RewriteRule ^(.*)$ http://new-example.com/$1 [L,R=301]
However, I will need to manually map some pages to their new equivalent pages like
Redirect 301 /about http://new-example.com/about-us
Is it possible to do both?
If you are going to use Rewrite, then use it only. I don't recommend use both mod-alias and mod-rewrite. You can use rewrite to map to individual pages too. Also the order matters. The catchall rule should be the last one. This would provide cleaner code IMO.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^new-example.com$ [NC]
RewriteRule ^about/?$ http://new-example.com/about-us [R=301,L]
RewriteCond %{HTTP_HOST} !new-example.com$ [NC]
RewriteRule ^(.*)$ http://new-example.com/$1 [L,R=301]

.htaccess redirect from domain to another

Ho can i redirect all requests for pattern host.com/* to otherhost.com/*
For example all request for host.com/page1 need to be rewritten to otherhost.com/page1
The difficult part is that requests to host.com itself (home page) shouls not be rewritten.
This is what i tried to do
RewriteRule ^(.*)$ http://otherhost.com/$1 [R=301,L]
But keeps rewriting all request including the homepage
Any help greatly appreciated
You should add some conditions :
RewriteEngine on
RewriteCond %{REQUEST_URI} !=/
RewriteCond %{REQUEST_URI} !^/index.html
RewriteRule ^(.*)$ http://otherhost.com/$1 [R=301,L]
You can use the following rewrite:
RewriteEngine on
RewriteCond %{HTTP_HOST} =host.com
RewriteCond %{REQUEST_URI} !=/
RewriteRule . http://otherhost.com%{REQUEST_URI} [R=301,NE,L]
RewriteCond %{HTTP_HOST} =host.com checks whether the host is host.com.
RewriteCond %{REQUEST_URI} !=/ checks whether the requested page is not the "index". (If this is what you mean by "requests to host.com itself (home page) shouls not be rewritten")
R=301 states it should send a HTTP 301 Moved Permanently header when redirecting.
NE states it should not escape special characters. (e.g. ? escaped to %3f)
This is important! If you don't specify this, many URLs will be wrong. (e.g. /blah.php?id=1 will be redirected to /blah.php%3fid%3d1, which is wrong.)
Try this one:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^(/index.html)?$
RewriteRule (.*) http://otherhost.com/$1 [R=301,L]

.htaccess - redirect domain name

I have a domain name www.domainA.com I want to redirect it to domainB in following manner.
www.domainA.com -> www.domainB.com
www.domainA.com/anything -> www.domainB.com/rebrand
How I can do this in htaccess, I have done following code but it redirecting to /rebrand/ only.
RewriteCond %{REQUEST_URI} ^\/
RewriteRule ^\/$ http://www.domainB.com/ [L,R=301]
RewriteCond %{HTTP_HOST} ^domainA\.com$ [NC]
RewriteRule ^(.*)$ http://www.domainB.com/rebrand/ [L,R=301]
URIs that go through rules in an htaccess file has the leading slash stripped off, so you can't match against it. For the second rule, it's matching the / request because the first rule isn't being applied and your regex matches anything or nothing, you can fix that by changing the * to +:
RewriteCond %{HTTP_HOST} ^domainA\.com$ [NC]
RewriteRule ^/?$ http://www.domainB.com/ [L,R=301]
RewriteCond %{HTTP_HOST} ^domainA\.com$ [NC]
RewriteRule ^(.+)$ http://www.domainB.com/rebrand/ [L,R=301]
Redirecting through htaccess is tricky sometimes, there are many ways of implementing this but there is one simple way which worked for me
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) [newdomain.com...] [R=301,L]
You can get more info from webmaster world forum

.htaccess url rewrite - remove .php remove www. convert ?id=10 to /10

As the title says, I need a quite complex url rewrite mechanism for a web-app as .htaccess rule.
I've searched quite a lot now and tried hundred of different rewrite rules.
So, basically this is what I need:
User goes to: http://www.site.com/product.php?id=12
Server should redirect to: http://site.com/product/12
Once thing to mention:
not all pages do append id's.
So I also have: http://www.site.com/some/page.php
which then should redirect to: http://site.com/some/page
or from http://site.com/anotherone.php to http://site.com/anotherone
You help is much appreciated and thank you a lot in advance for you help!
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)\.php$ http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)\.php\?id=([0-9]+)$ http://%1/$1/$2 [R=301,L]
I supose that you, previously have this mod_rewrite rule active:
Users goes to http://site.com/product/12 and in the browser is showed this URL, and internaly, and only internaly, server serve http://www.site.com/product.php?id=12
Put the first RewriteCond and Rule this:
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^(\w+)\.php\?id=(\d*)$ /$1/$2 [R=301]
And add another to remove the .php when ends with .php
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^(\w+)\.php$ /$1 [R=301]