MyBB Community Forums

Full Version: Redirect Issue with .htaccess
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
It turns out I am facing a redirect issue with the current version (1.8.10). I tried getting help from my hosting providers but they weren't able to fix the problem and told me to report the issue instead.

My .htaccess Settings are:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^warrior.pk$ 
RewriteRule .* https://www.warrior.pk%{REQUEST_URI} [R,L]


It works fine redirecting non-www to www over HTTPS but as soon as I add rule for www there's an infinite loop. For example the setting for both non-www and www would be:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^warrior.pk$ [OR]
RewriteCond %{HTTP_HOST} ^www.warrior.pk$
RewriteRule .* https://www.warrior.pk%{REQUEST_URI} [R,L]


Any suggestions to fix the problem?
I have a feeling it has to do with the implicit and and then explicit or in your rules (basically an order of operations issue, I'm thinking), but I'm no expert on Apache's RewriteCond handling.

Even though it's a bit uglier, perhaps try
RewriteEngine On

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^warrior.pk$
RewriteRule .* https://www.warrior.pk%{REQUEST_URI} [R,L]

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www.warrior.pk$
RewriteRule .* https://www.warrior.pk%{REQUEST_URI} [R,L]

Which basically changes the logic from IF no-HTTPS and $host1 or $host2, then rewrite to IF no-HTTPS and $host1, rewrite.... if no-HTTPS and $host2, then rewrite.
(2017-01-22, 05:27 AM)Josh H. Wrote: [ -> ]I have a feeling it has to do with the implicit and and then explicit or in your rules (basically an order of operations issue, I'm thinking), but I'm no expert on Apache's RewriteCond handling.

Even though it's a bit uglier, perhaps try
RewriteEngine On

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^warrior.pk$
RewriteRule .* https://www.warrior.pk%{REQUEST_URI} [R,L]

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www.warrior.pk$
RewriteRule .* https://www.warrior.pk%{REQUEST_URI} [R,L]

Which basically changes the logic from IF no-HTTPS and $host1 or $host2, then rewrite   to   IF no-HTTPS and $host1, rewrite.... if no-HTTPS and $host2, then rewrite.

Thank you for replying but it didn't work either. Here's the .htaccess:


 Options -MultiViews +FollowSymlinks -Indexes


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

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^warrior.pk$ 
RewriteRule .* https://www.warrior.pk%{REQUEST_URI} [R,L]

#
# If mod_security is enabled, attempt to disable it.
# - Note, this will work on the majority of hosts but on
#   MediaTemple, it is known to cause random Internal Server
#   errors. For MediaTemple, please remove the block below
#
<IfModule mod_security.c>
	# Turn off mod_security filtering.
	SecFilterEngine Off

	# The below probably isn't needed, but better safe than sorry.
	SecFilterScanPOST Off
</IfModule>

#
# MyBB "search engine friendly" URL rewrites
# - Note, for these to work with MyBB please make sure you have
#   the setting enabled in the Admin CP and you have this file
#   named .htaccess
#
<IfModule mod_rewrite.c>
	RewriteEngine on
	RewriteRule ^forum-([0-9]+)\.html$ forumdisplay.php?fid=$1 [L,QSA]
	RewriteRule ^forum-([0-9]+)-page-([0-9]+)\.html$ forumdisplay.php?fid=$1&page=$2 [L,QSA]

	RewriteRule ^thread-([0-9]+)\.html$ showthread.php?tid=$1 [L,QSA]
	RewriteRule ^thread-([0-9]+)-page-([0-9]+)\.html$ showthread.php?tid=$1&page=$2 [L,QSA]
	RewriteRule ^thread-([0-9]+)-lastpost\.html$ showthread.php?tid=$1&action=lastpost [L,QSA]
	RewriteRule ^thread-([0-9]+)-nextnewest\.html$ showthread.php?tid=$1&action=nextnewest [L,QSA]
	RewriteRule ^thread-([0-9]+)-nextoldest\.html$ showthread.php?tid=$1&action=nextoldest [L,QSA]
	RewriteRule ^thread-([0-9]+)-newpost\.html$ showthread.php?tid=$1&action=newpost [L,QSA]
	RewriteRule ^thread-([0-9]+)-post-([0-9]+)\.html$ showthread.php?tid=$1&pid=$2 [L,QSA]

	RewriteRule ^post-([0-9]+)\.html$ showthread.php?pid=$1 [L,QSA]

	RewriteRule ^announcement-([0-9]+)\.html$ announcements.php?aid=$1 [L,QSA]

	RewriteRule ^user-([0-9]+)\.html$ member.php?action=profile&uid=$1 [L,QSA]

	RewriteRule ^calendar-([0-9]+)\.html$ calendar.php?calendar=$1 [L,QSA]
	RewriteRule ^calendar-([0-9]+)-year-([0-9]+)-month-([0-9]+)\.html$ calendar.php?calendar=$1&year=$2&month=$3 [L,QSA]
	RewriteRule ^calendar-([0-9]+)-year-([0-9]+)-month-([0-9]+)-day-([0-9]+)\.html$ calendar.php?action=dayview&calendar=$1&year=$2&month=$3&day=$4 [L,QSA]
	RewriteRule ^calendar-([0-9]+)-week-(n?[0-9]+)\.html$ calendar.php?action=weekview&calendar=$1&week=$2 [L,QSA]

	RewriteRule ^event-([0-9]+)\.html$ calendar.php?action=event&eid=$1 [L,QSA]

	<IfModule mod_env.c>
		SetEnv SEO_SUPPORT 1
	</IfModule>
</IfModule>

#
# If Apache is compiled with built in mod_deflade/GZIP support
# then GZIP Javascript, CSS, HTML and XML so they're sent to
# the client faster.
#
<IfModule mod_deflate.c>
	AddOutputFilterByType DEFLATE text/css text/html application/xhtml+xml text/xml application/xml text/plain text/x-component application/javascript application/x-javascript application/rss+xml application/atom+xml application/json application/manifest+json application/x-web-app-manifest+json application/vnd.ms-fontobject application/font-sfnt application/font-woff application/font-woff2 image/svg+xml image/x-icon
</IfModule>

# Note: You are able to choose a different name in the Admin CP. If you've done that you need to change it here too
<Files "error.log">
	Order Deny,Allow
	Deny from all
</Files>



Edit: Nevermind, here's the fix:

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

I believe the loop was because of reverse proxy. Cloudflare, I guess?
(2017-01-22, 06:43 PM)Aleri0n Wrote: [ -> ]I believe the loop was because of reverse proxy. Cloudflare, I guess?

Oh yeah. That would definitely do it.