XF 2.2 Redirecting requests to deleted threads

Anatoliy

Well-known member
I deleted a bunch of old (and I thought useless) threads. Now my SEO tool shows that a dozen of them are in Google's SERP, they keep getting visitors from Google, but return 404, which is not good and has to be fixed.

So my question: what is the best way to set 301 redirects for 20-30 urls?
 
Solution
I use .htaccess e.g.


Code:
Redirect 301 /threads/deleted-thread.101 https://yourforum.com/threads/redirect-to-this-one.102

Each redirect on a new line and note the space before https

bzcomputers

Well-known member
Bearing in mind that I have a VPS with super tech support, I don't even need to do it by myself, but just to write a request.
So what exactly should I write to them? That I want them to move those 301 & 410 redirect rules from .htaccess to the server config?
You would add something like this to your Apache Post VirtualHost include...

Code:
<IfModule mod_rewrite.c>
<Directory "/home/XXXyoursitedirectoryhereXXX/public_html">
Options -Indexes
AllowOverride None

RewriteEngine On

# Force SSL
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Force www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [E=HTTPS,R=301,L]

# XenForo
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</Directory>

</IfModule>

If you have XenForo in a subdirectory like /community it would look like this:

Code:
<IfModule mod_rewrite.c>
<Directory "/home/XXXyoursitedirectoryhereXXX/public_html">
Options -Indexes
AllowOverride None

RewriteEngine On

# Force SSL
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Force www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [E=HTTPS,R=301,L]
</Directory>

<Directory "/home/XXXyoursitedirectoryhereXXX/public_html/community">
RewriteEngine On

# XenForo
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</Directory>

</IfModule>

Make changes as necessary for your specific installation. Your custom rules will go right above the line starting with "# XenForo". Once this is in place and working well, you can delete your .htaccess files from the directory structure they are now completely ignored. Your redirects will now be cached by the server and you have eliminated the performance loss from Apache AllowOverride.
 

Mr Lucky

Well-known member
You would add something like this to your Apache Post VirtualHost include...

Code:
<IfModule mod_rewrite.c>
<Directory "/home/XXXyoursitedirectoryhereXXX/public_html">
Options -Indexes
AllowOverride None

RewriteEngine On

# Force SSL
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Force www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [E=HTTPS,R=301,L]

# XenForo
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</Directory>

</IfModule>

If you have XenForo in a subdirectory like /community it would look like this:

Code:
<IfModule mod_rewrite.c>
<Directory "/home/XXXyoursitedirectoryhereXXX/public_html">
Options -Indexes
AllowOverride None

RewriteEngine On

# Force SSL
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Force www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [E=HTTPS,R=301,L]
</Directory>

<Directory "/home/XXXyoursitedirectoryhereXXX/public_html/community">
RewriteEngine On

# XenForo
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</Directory>

</IfModule>

Make changes as necessary for your specific installation. Your custom rules will go right above the line starting with "# XenForo". Once this is in place and working well, you can delete your .htaccess files from the directory structure they are now completely ignored. Your redirects will now be cached by the server and you have eliminated the performance loss from Apache AllowOverride.
Why didn't I think of that - easy!
 

Anatoliy

Well-known member
Once this is in place and working well, you can delete your .htaccess files from the directory structure they are now completely ignored.
I passed them your code. (didn't add 301 redirects from htaccess). they reported that it's done. I'm checking and those 301 redirects still works. Does it mean they did something wrong as you said that htaccess will be ignored? is it possible to check somehow that they did the work correctly?
 

bzcomputers

Well-known member
I passed them your code. (didn't add 301 redirects from htaccess). they reported that it's done. I'm checking and those 301 redirects still works. Does it mean they did something wrong as you said that htaccess will be ignored? is it possible to check somehow that they did the work correctly?
Yes, you are correct if the rules weren't added to the server config they should not be working right now. Make sure they restart Apache, it is required anytime a change is made to the server config.
 

Masetrix

Well-known member
Make sure they restart Apache, it is required anytime a change is made to the server config.
The advantage of using the server config is that these instructions are always kept in RAM - hence the Apache restart after changes - while the htaccess has to be read in from the file system every time it is used.

Therefore I no longer use htaccess but all entries in the server config.
This saves a lot of I / O for disk access and creates I/O "space" for Mysql :)
 
Last edited:
Top