Home > database >  Rewriting to add "www" failing along with internal rewrites in htaccess
Rewriting to add "www" failing along with internal rewrites in htaccess

Time:08-06

This is the htaccess for my website currently:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^(article/?).*$
RewriteRule ^article/?(.*)$ article.php?$1 [L, QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^(info/?).*$
RewriteRule ^info/?(.*)$ info.php?$1 [L, QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^(admin/?).*$
RewriteRule ^admin/?(.*)$ admin.php?$1 [L, QSA]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L, QSA]


# Force to SSL
RewriteCond %{HTTP:HTTPS} !1
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301, L]

# Force to WWW
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301, L]

If the cache is cleared on the browser and I attempt to load "rotak.ro/article/rain", it redirects to "https://rotak.ro/article/rain", and I therefore fail to load external php css and java files because of the missing www. If instead I load "rotak.ro", it correctly adds the www to the start of the url.

I'm assuming the RewriteCond for the WWW is failing to confirm in the cases where there are multiple slashes in the url, but I don't understand what's going on wrongly.

I'd like every url written, so rotak.ro/article/rain or rotak.ro/info/cookies to redirect to https://wwww.rotak.ro/ ....

How should I go about fixing this issue?

CodePudding user response:

Have your htaccess rules file in following manner. You need to change order of redirect and www implementation rules. Then we need not to have multiple rules for your internal rewrite that could be handled within a single rules set itself.

Make sure to clear your browser cache before testing your URLs.

RewriteEngine ON  

#Force to SSL
RewriteCond %{HTTP:HTTPS} !1
RewriteRule ^(.*)/?$ https://%{HTTP_HOST}/$1 [R = 301, L, NE]

#Force to WWW
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)/?$ https://www.%{HTTP_HOST}/$1 [L, NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(article|info|admin)/?(.*)/?$ $1.php?$2 [NC,L, QSA]

NOTE: When we are putting redirection 301 in www rules it was affecting internal rules, so we had to remove it off. Even for OP internal rewrites were NOT working with OP's attempts.

  • Related