1

Recently, I enforced non-www to www redirection on my website.

I am using these rules in my htaccess file:

Options -Indexes
Options +FollowSymLinks

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault A1209600
  ExpiresByType text/html A1
</IfModule>

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

ErrorDocument 401 /error.php
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php

It works well except that people coming form this link structure:

https://example.com/blog/9999/titlequerystring

get redirected that way by the server:

https://example.com/blog/9999/titlequerystring

then--

https://example.com/blog.php/9999/titlequerystring

then--

https://www.example.com/blog.php/9999/titlequerystring

The .php after "blog" brings users to error page no found.

It should redirect like that to work properly:

https://www.example.com/blog/9999/titlequerystring

This is a custom CMS. Not Wordpress. It uses Multiviews to work properly. My server is set with Apache 2.4+PHP 7.4 and PHP-FPM as handler.

I've been trying to fix this for a few days but I guess I am not skilled enough to diagnose it further.

Thanks.

As replied to CBroe, here is the htaccess file for mod_rewrite without Multiviews:

Options -Indexes
Options +FollowSymLinks

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault A1209600
  ExpiresByType text/html A1
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* loader.php [L,QSA]
</IfModule>

ErrorDocument 401 /error.php
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php

loader.php: https://controlc.com/47512763

ben
  • 31
  • 4
  • Instead of `%{REQUEST_URI}`, try to capture the matched path in the rule, and use that for the substitution: `RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [L,R=301]` – CBroe Jan 05 '21 at 07:56
  • Thanks for your comment CBroe. I just had a try with your rule but without success. – ben Jan 05 '21 at 08:23
  • 1
    First of all, make sure that your browser cache isn’t fooling you now. You used `R=301`, so these redirects will get cached. Test in a private browser window, or clear your history. (Always advisable to use 302 during testing first.) If that does not help, then show us what else your .htaccess contains. – CBroe Jan 05 '21 at 08:27
  • Yes, I cleared the cache before to test the new rule. I don't have much in my .htaccess but I updated my post with the full .htaccess content. Thanks – ben Jan 05 '21 at 09:07
  • Looks like MultiViews is interfering here, before the rewriting even happens? Not sure in which order these things are supposed to happen (and too lazy to go look it up right now.) I would recommend to replace that with a rewrite solution a la https://stackoverflow.com/questions/4908122/removing-the-php-extension-with-mod-rewrite – CBroe Jan 05 '21 at 09:23
  • Actually, the CMS also gives the choice to use mod_rewrite only, without Multiviews. But it's not working properly. I am gonna update my post with the rules for you. – ben Jan 05 '21 at 09:38
  • @CBroe I updated my post. The issue I am getting with the second htaccess+loader.php without Multiviews is that all the page like https://www.example.cam/blog are not loading at all. The loader.php seems ignored. That's why I gave up and went for Multiviews. – ben Jan 05 '21 at 09:49
  • Try and explicitly _disable_ MultiViews, otherwise it might still be active from the default server configuration. – CBroe Jan 05 '21 at 10:00
  • I turn it off from Apache Configuration panel in WHM. Then Apache is restarted. Is that enough? I now when it's off because any .php file is not accessible anymore without his extension. – ben Jan 05 '21 at 10:09
  • Not sure what exactly is going on, always hard to tell from the outside. Would recommend to enable rewrite logging, and then see what exactly is happening internally. – CBroe Jan 05 '21 at 10:14
  • I used "LogLevel alert rewrite:trace3" in vhost but as my site has constantly around 200 users online... the log file is just huge and unreadable. – ben Jan 05 '21 at 12:06

0 Answers0