Friendly URLs and https redirect - not working!

Discuss practical ways rearrange URLs using mod_rewrite.

Friendly URLs and https redirect - not working!

Postby kevster76 » Tue Sep 08, 2009 1:01 am

Hi,

I've been stuck on this for a few days now and no search has helped me. I'm sure there's a really simple solution, but at the minute I can't seem to find it! I hope you guys can help. :)

On one of my sites I have an .htaccess file that redirects to pretty URLs (for placing in an email). This works fine:

Code: Select all
Options +FollowSymLinks

RewriteEngine On
Options -Multiviews

RewriteRule ^blah/([a-zA-Z0-9+-]+)/([a-zA-Z0-9+-]+)(/)?$ /blah.php?company=$1&key=$2 [NC]
RewriteRule ^blah/([a-zA-Z0-9+-]+)(/)?$ /blah.php?company=$1 [NC]
RewriteRule ^blah(/)?$ /blah.php [NC]


However, we've decided to make the whole site work on https (as it holds personal information and users can login from any page) and I've added some code to the .htaccess for this. The whole .htaccess file now looks like:

Code: Select all
Options +FollowSymLinks

RewriteEngine On
Options -Multiviews

RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

RewriteRule ^blah/([a-zA-Z0-9+-]+)/([a-zA-Z0-9+-]+)(/)?$ /blah.php?company=$1&key=$2 [NC]
RewriteRule ^blah/([a-zA-Z0-9+-]+)(/)?$ /blah.php?company=$1 [NC]
RewriteRule ^blah(/)?$ /blah.php [NC]


Whilst the site is correctly redirecting to the secure pages, the $_GET variables aren't being passed through to blah.php (the rewritten URL). Although if I echo $_SERVER['REQUEST_URI'] from the blah.php page, it displays correctly - the values that I entered (e.g. /blah/companyname)

Am I doing something really stupid? I'd really appreciate your help guys.. really stumped on this one!

Thanks in advance,
Kev
kevster76
 
Posts: 4
Joined: Tue Sep 08, 2009 12:39 am

Postby richardk » Tue Sep 08, 2009 7:52 am

It sounds like MultiViews, even though you have
Code: Select all
Options -Multiviews

Do you get the same result if you remove the ^blah/ RewriteRules?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby kevster76 » Tue Sep 08, 2009 11:08 am

Hi Richard,

Thanks for the reply.

If I go directly to www.domain.com/blah.php?company=foo (both http and https) that works fine - the company is passed to the PHP script.

If I remove the following:

Code: Select all
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]


The rewrite works correctly using http, but using https the $_GET variables aren't passed...

I've also noticed that when it's failing, some of the $_SERVER vars aren't as I expected:

REQUEST_URI: /blah/companyname (as expected)
PATH_TRANSLATED: /var/www/companyname (expected /var/www/blah)
PHP_SELF: /invite.php/websushi (expected /invite.php)

*scratching head* any other ideas? :)

Thanks,
Kev


P.S. Your post made me realise that I wasn't capitalising the V in MultiViews - but I changed that and it didn't seem to make any difference...
kevster76
 
Posts: 4
Joined: Tue Sep 08, 2009 12:39 am

Postby richardk » Tue Sep 08, 2009 4:29 pm

If you put the following in your .htaccess file
Code: Select all
Options +FollowSymLinks -MultiViews

RewriteEngine On

RewriteRule ^blah/([0-9]+)/?$ /blah.php?number=$1 [NC,QSA,L]

and go to /blah/123 (with http and https) you go to /blah.php and $_GET['number'] is set, correct?

And if you add
Code: Select all
RewriteCond %{HTTPS} !=on [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

after
Code: Select all
RewriteEngine On

and go to /blah/123 (with http (causing a redirect) and https) you go to /blah.php but $_GET['number'] is not set, correct?

I've also noticed that when it's failing, some of the $_SERVER vars aren't as I expected:

REQUEST_URI: /blah/companyname (as expected)
PATH_TRANSLATED: /var/www/companyname (expected /var/www/blah)
PHP_SELF: /invite.php/websushi (expected /invite.php)

What configuration did you add/change to get SSL working?

Your post made me realise that I wasn't capitalising the V in MultiViews - but I changed that and it didn't seem to make any difference...

It doesn't make a difference. As far as i know all directives are case insensitive.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby kevster76 » Tue Sep 08, 2009 11:17 pm

Thanks for the reply Richard - really appreciate your help!

I started with a blank .htaccess file and made your suggested changes.

Without the rewrite condition:
http://test.com/blah/123 - the number was passed to the script
https://test.com/blah/123 - script loaded, but number NOT passed

With the rewrite condition http successfully redirects to the https version, but no number passed.

So it doesn't seem to be an issue with the https redirect?

What configuration did you add/change to get SSL working?


I'm not sure I understand the question? We purchased and installed a certificate, and I think the server guy did some config. Do you think we're missing something in the configuration?

Cheers,
Kev
kevster76
 
Posts: 4
Joined: Tue Sep 08, 2009 12:39 am

Postby richardk » Wed Sep 09, 2009 12:12 pm

I think the SSL request does not process the .htaccess file. You will need to ask your host.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby kevster76 » Wed Sep 09, 2009 12:17 pm

Thanks Richard.

Code: Select all
I think the SSL request does not process the .htaccess file. You will need to ask your host.


I'll speak with the server admin and post back when we have a solution! Many thanks for your help in hopefully identifying the cause!
kevster76
 
Posts: 4
Joined: Tue Sep 08, 2009 12:39 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 9 guests

cron