How to make a 301 redirect in OLS

#1
Hello,

I have noticed that I have some 404 errors on my website. I have tried to fix them by putting the following instruction in the .htaccess but it does not work:

Redirect 301 /urlwith404 https://mydomain.com/home

Investigating through the forum, I have seen that OLS does not accept 'Redirect 301' as instruction, but that it must be done with 'Rewrite Rules'. So, I've done the following redirection, but it only works if I put it above of the RewriteRule of the Wordpress index:

RewriteEngine On
RewriteRule /urlwith404/ https://mydomain.com/home [L,R=301]

Can you tell me why it only works if it's on above of the Wordpress Rewrite Rule?

I have also tried to do the redirects creating a context of type 'redirect' in the virtual host, but it does not work.

Admin Control Panel -> Virtual Hopt -> MyVirtualHost -> Context Tab -> Add -> Redirect Type:

URI: /urlwith404/
External redirect: No
Status Code: 301 Moved permanently
Destination URI: https://mydomain.com/home

I have also tried to do the redirections creating a context of type 'static' in the virtual host, but it does not work either.

Admin Control Panel -> Virtual Hopt -> MyVirtualHost -> Context Tab -> Add -> Static Type:

URI: /
Location: $VH_ROOT/html/
Accesible: Yes
Enable Rewrite: Yes
Rewrite Rule: RewriteRule /urlwith404/ https://mydomain.com/home [L,R=301]

What am I doing wrong? Can you help me or explain how to do it?
 
#3
Hi Pong,

What you have sended to me, I saw yesterday before opening this thread. That's why I managed to make redirects. But I do not understand why the redirects do not work if I try to it from a context. I do not know what goes wrong.

Thanks.
 

David

Active Member
#4
Hi iRafaelG,

Can you do this way, update the vhost rewrite and add some rules to the rules box.

Such as in the conf file you will see below block
rewrite {
enable 1
logLevel 9
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
}

This way should work.

Thanks.
David
 

David

Active Member
#5
For context level, I tested and it also woks.
The config is as

context / {
rewrite {
enable 1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /i.html [L]
}

}
 
#6
Hi iRafaelG,

Can you do this way, update the vhost rewrite and add some rules to the rules box.

Such as in the conf file you will see below block
rewrite {
enable 1
logLevel 9
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
}

This way should work.

Thanks.
David
Hi David,

Thanks for the info. Very good.
Currently, in the 'Rewrite' tab of the Virtual Vost I have the following configuration:

Enable Rewrite: Yes
Rewrite Rules: rewriteFile /usr/local/lsws/TyV/html/.htaccess

According to what you have written, in the 'Rewrite Rules' section I would have to remove 'rewriteFile /usr/local/lsws/TyV/html/.htaccess'?
That is to say:

Enable Rewrite: Yes
Log Level : 9
Rewrite Rules: rewriteFile /usr/local/lsws/TyV/html/.htaccess RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L]

Thanks!
 

David

Active Member
#7
Actually the rewritefile is like `include`, it will expand the following file and put the content in that place.
If you do not have /usr/local/lsws/TyV/html/.htaccess or it is empty, you can remove this line in the section.
And you replacement is good.
 
#8
For context level, I tested and it also woks.
The config is as

context / {
rewrite {
enable 1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /i.html [L]
}

}
In this case, it would be:

Admin Panel Control -> Virtual Host -> Context Tab -> Add -> Type: Static

URI: /
Location: $VH_ROOT/html/
Accessible: Yes
Enable Rewrite: Yes
Rewrite Rule: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /i.html [L]

Right?

And the redirections would go just below 'RewriteRule . /i.html [L]', right?

That is to say:

URI: /
Location: $VH_ROOT/html/
Accessible: Yes
Enable Rewrite: Yes
Rewrite Rule: RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /i.html [L]

RewriteRule /url1with404 https://mydomain/home
RewriteRule /url2with404 https://mydomain/foo
...

Thanks very much!
 
#10
What are the /url1with404 and /url2with404 mean?
Is this your real rules?
If yes, they won't work.
They are examples of pages with status or error 404 that I want to redirect to the 'Home' of my web. It should be added to the end of each one '[L,R=301]'. I just realized that I have not put it in the previous post. Sorry.

Thanks David!
 

David

Active Member
#11
So you want the 404 error page of different context in the same vhost will be redirected to different files?
You may do this way

context /url1 {
rewrite {
enable 1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /home.html [L]
}

}



context /url2 {
rewrite {
enable 1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /foo.html [L]
}

}
 
#13
Hi David,

I have not managed to do redirects 301 of the pages with error 404 through contexts like we talked the other day.

The only way that redirects work for me, is writing 'RewriteEngine On RewriteRule /urlwith404/ https://mydomain.com [L,R=301]' in the .htaccess above 'IfModule mod_rewrite.c' of Wordpress.

Thank you very much for your help.
 

David

Active Member
#14
OK.
If you solve it, please paste your rules here.
If not, we can work together to solve it.
My sample is for you want to redirect to different URL.
 
#15
Hi again,

My situation is this:

Two months ago I bought the domain 'tecnologiayvideojuegos.com', but before it was mine, it was from another person who was using it. Then, in Google Search Console, I see several old URLs that are not mine and have 404 status because they do not already exist.

That's why I want to redirect them to the 'Home'. An example of redirection in .htaccess that has worked for me above the 'mod_rewrite.c ' of Wordpress is this:

RewriteEngine On
RewriteRule /top-2017/ https://tecnologiayvideojuegos.com [L,R=301]

I hope to help other people who are in my same situation with the example :)

Best regards.
 
Top