Reputation: 2116
I am working on a project in which i have migrated old to new site. SEO is still working for old site so i have written rules in my yii2 application as
'rules' => [
"<year:\d{4}>/<number1:\d{2}>/<number2:\d{2}>/<slug>"=>'video/parse',
This yii2 redirection is working fine for URLS not ending in trailing slash. But this rule fails for trailing slash URLs. To cope with this problem I tried to redirect through apache. To do so i write this code
<VirtualHost *:80>
RewriteEngine on
RewriteCond %{SERVER_NAME} =mydomain.org
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
Problem1:
When I hit http://example.org/2017/09/10/slug/ then it is redirected to http://example.org//2017/09/10/slug/ as you can see an additional slash is added after domain name.
Problem2:
When I hit https://example.org/2017/09/10/slug/ then it is not redirected anyway.
Questions:
1. How do we redirect for trailing slash?
2. How to get the redirection working for https?
Upvotes: 0
Views: 152
Reputation: 22174
You should probably use UrlNormalizer
for such redirections:
'urlManager' => [
// ...
'normalizer' => [
'class' => yii\web\UrlNormalizer::class,
// you can use temporary redirection instead of permanent for tests
// 'action' => UrlNormalizer::ACTION_REDIRECT_TEMPORARY,
],
],
You can read more about URL normalization in this guide article.
Upvotes: 1
Reputation: 42959
The issue you face is that you are actually not using a dynamic configuration file (".htaccess") as you claim (I assume that, since the VirtualHost
directive is invalid in there). And because the RewriteRule
logic works on absolute paths when used in the real http server configuration files as opposed to relative paths when used in dynamic configuration files. This is documented pretty prominently, actually. That difference in logic results in your capture (which you reuse with the $1
reference) to contain an actual leading slash.
So all you need to do is to change is your rule to this more robust version:
RewriteRule ^/?(.*)/$ /$1 [R=301,L]
That leading ^/?
allows an unmodified rule to be used likewise in both versions of configuration files with identical logic. The reason for that should be clear, I assume.
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
Upvotes: 0