Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect with .htaccess not working correctly when missing / in the end

I've Wordpress site with WPML installed. I've done 2 things recently:

  • Move HTTP only to HTTPS only
  • Move http://domain.pl to https://domain.xyz (English)
  • Move http://domain.pl/pl to https://domain.pl (Polish)

This is done to improve SEO. However while I got that working without much problem using:

 # BEGIN HTTPS
 <IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteCond %{SERVER_PORT} ^80$
 #RewriteCond %{HTTPS} !^on$
 #RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
 RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
 </IfModule>
 # END HTTPS
 # BEGIN WordPress
 <IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /
 RewriteRule ^index\.php$ - [L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule . /index.php [L]
 </IfModule>

 # END WordPress

In apache.conf file ServerName domain.xyz, ServerAlias domain.pl www.domain.pl and so on. I'm having problems with most of my old blog posts that are around the internet in different forms. For example:

  • This little entry http://domain.pl/email-address-policy/ redirects properly to https://domain.xyz/email-address-policy/
  • This little entry http://domain.pl/powershell-write-host doesn't. It simply goes to https://domain.pl/powershell-write-host which is 404 and that's it.

It seems that last / is making huge difference.

I've planned to add all redirects in even direct form 1 to 1 to new domain but since it's partially working, partially not I'm kind of lost... and I am not sure how I could translate them correctly.

EDIT:

I've tried multiple options, even one that supposedly rewrites all links to end with / and it still fails.

# BEGIN HTTPS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#RewriteCond %{SERVER_PORT} ^80$
#RewriteCond %{HTTPS} !^on$
#RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
#RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
#RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
#RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} !^on$
#RewriteRule (.*) https://evotec.xyz/$1 [R=301,L]
#RewriteRule (.*) https://%{SERVER_NAME%}/$1 [R=301,L]
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_URI} !(.*)/$
#RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1/ [L,R=301]
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpg|png|jpeg|css|js)$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1/ [L,R=301]


</IfModule>
# END HTTPS

Another update:

  • https://domain.xyz/url-is-here - works correctly
  • https://domain.xyz/url-is-here/ - works correctly
  • https://domain.pl/url-is-here - doesn't work, isn't convered to https://domain.xyz/url-is-here/
  • https://domain.pl/url-is-here/ - convers correctly to https://domain.xyz/url-is-here/

So it's not entirely not working for all slash vs non-slash. It's more about translation of some sort that doesn't happen if it points to old domain.

I've tried using Redirect Checker to see how it works (on proper example) but I can't make any meaning from it.

  • Check of http://evotec.pl/hub/scripts/office365-addbulkemailaddressses-ps1

http://evotec.pl/hub/scripts/office365-addbulkemailaddressses-ps1 301 Moved Permanently https://evotec.pl/hub/scripts/office365-addbulkemailaddressses-ps1/ 301 Moved Permanently https://evotec.xyz/hub/scripts/office365-addbulkemailaddressses-ps1/ 200 OK

  • Check of http://evotec.pl/hub/scripts/office365-addbulkemailaddressses-ps1/

http://evotec.pl/hub/scripts/office365-addbulkemailaddressses-ps1/ 301 Moved Permanently https://evotec.xyz/hub/scripts/office365-addbulkemailaddressses-ps1/ 200 OK

  • Check of https://evotec.pl/hub/scripts/office365-addbulkemailaddressses-ps1

https://evotec.pl/hub/scripts/office365-addbulkemailaddressses-ps1 404 Not Found

The more options I try the weirder it gets. While the RedirectChecker seems to be showing one thing ... browser is behaving a bit different and doesn't work for the first entry anyways causing 404 straight away

Edit:

If I leave only "Wordpress" data following is true:

  • http://evotec.pl/hub/scripts/office365-addbulkemailaddressses-ps1 gets 404 http://evotec.pl/hub/scripts/office365-addbulkemailaddressses-ps1 exactly as is
  • http://evotec.pl/hub/scripts/office365-addbulkemailaddressses-ps1/ gets https://evotec.xyz/hub/scripts/office365-addbulkemailaddressses-ps1/
  • https://evotec.pl/hub/scripts/office365-addbulkemailaddressses-ps1 gets 404 exactly as is

It only works properly if i use the correct/new domain then all works properly:

  • https://evotec.xyz/hub/scripts/office365-addbulkemailaddressses-ps1
  • https://evotec.xyz/hub/scripts/office365-addbulkemailaddressses-ps1
  • http://evotec.xyz/hub/scripts/office365-addbulkemailaddressses-ps1
  • http://evotec.xyz/hub/scripts/office365-addbulkemailaddressses-ps1/

I guess if I can't redirect it, i will leave it as it is and just give up.

like image 338
MadBoy Avatar asked Aug 06 '16 12:08

MadBoy


People also ask

Why is my .htaccess file not working?

Improper syntax being used It is quite common for a syntax error to be the reason for an . htaccess file not working. If you are familiar with how to read and configure . htaccess rules, double check your configuration.

Does .htaccess do redirect?

Use a 301 redirect . htaccess to point an entire site to a different URL on a permanent basis. This is the most common type of redirect and is useful in most situations. In this example, we are redirecting to the "example.com" domain.

How do you check my htaccess is working or not?

Save the file and type the URL yoursite.com/foobar/ . If the reditect works and the URL gets redireted to the homepage of example.com then it's clear that your htaccess is working and being read by your Apache server. If it still doesn't work then the problem might be that your hosting provider has not enabled it.

How long does it take for htaccess to change effect?

htaccess files follow the same syntax as the main configuration files. Since . htaccess files are read on every request, changes made in these files take immediate effect.


1 Answers

I've actually solved it by using SEO Redirection Premium plugin for Wordpress.

enter image description here

It's able to redirect broken links without /. In this case I've done it manually but I'm working on a way to do it in more global way with Regex. Just need to find proper one.

enter image description here

like image 95
MadBoy Avatar answered Sep 28 '22 13:09

MadBoy