How to redirect via . htaccess?

Asked

Viewed 2,336 times

4

I’d like to redirect 15 to 20 urls static from one site to another url, type:

http://meusite.com.br/home1.html for http://novosite.com.br
http://meusite.com.br/home2.html for http://novosite.com.br
http://meusite.com.br/home3.html for http://novosite.com.br
http://meusite.com.br/home4.html for http://novosite.com.br
http://meusite.com.br/home5.html for http://novosite.com.br

How can I do this with . htaccess?

1 answer

6


You must use the flag [R=301] or [R=302] only to indicate the type of direction.

In the case if 301 is a permanent redirect, if 302 then is temporary redirect.

On the website meusite.com.br you must create in the root folder a . htaccess with the following content:

RewriteRule ^home1\.html$ http://novosite.com.br [R=301]
RewriteRule ^home2\.html$ http://novosite.com.br [R=301]
RewriteRule ^home3\.html$ http://novosite.com.br [R=301]
RewriteRule ^home4\.html$ http://novosite.com.br [R=301]
RewriteRule ^home5\.html$ http://novosite.com.br [R=301]
RewriteRule ^home6\.html$ http://novosite.com.br [R=301]

If the paths of the urls on the new site are the same as the old one, you should do so:

RewriteRule ^home1\.html$ http://novosite.com.br/$0 [R=301]
RewriteRule ^home2\.html$ http://novosite.com.br/$0 [R=301]
RewriteRule ^home3\.html$ http://novosite.com.br/$0 [R=301]
RewriteRule ^home4\.html$ http://novosite.com.br/$0 [R=301]
RewriteRule ^home5\.html$ http://novosite.com.br/$0 [R=301]
RewriteRule ^home6\.html$ http://novosite.com.br/$0 [R=301]

The $0 already identifies the url path because of regex

If all urls have this default home1.html to home20.html, you can simplify to:

RewriteRule ^home(\d+)\.html$ http://novosite.com.br/$0 [R=301]

the \d+ represents "any number", so it will recognize home1.html or home10001.html for example.

You can change [R=301] for [R=302] should the redirect be temporary and in the future return to the old site.

Note that you may also want to use the flag [QSA] chance want to direct urls with querystring, for example if access meusite.com.br/home1.html?foo=a it should redirect to novosite.com.br/home1.html with . htaccess

But if you use the flag [QSA] then the redirect will look like this novosite.com.br/home1.html?foo=a

For this use thus:

RewriteRule ^home(\d+)\.html$ http://novosite.com.br/$0 [QSA,R=301] 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.