Forum Moderators: phranque

Message Too Old, No Replies

http to https on 2 urls

         

Itworx4me

12:58 am on Feb 3, 2009 (gmt 0)

10+ Year Member



I am tring to use this code in the htaccess file to rewrite two pages to use the https extension.

RewriteEngine On
RewriteRule ^([^/]*)\.html$ /index.php?pageid=$1 [L]

RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} GET
RewriteCond %{REQUEST_URI} (grilling_menu¦buffet_menu)\.html
RewriteRule ^(.*)$ [domain-name.com...] [L,R301]

RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} GET
RewriteCond %{REQUEST_URI} !(grilling_menu¦buffet_menu)\.html
RewriteRule ^(.*)$ [domain-name.com...] [L,R301]

The 2 urls are
www.domain-name.com/buffet_menu.html
www.domain-name.com/grilling_menu.html

Which are being rewritten from the first rewrite rule that looks like this:
index.php?pageid=grilling_menu
index.php?pageid=buffet_menu

I only want the https on the 2 urls above and not on the rest of the site. Can someone point me in the right direction on how to get this to work?

Thanks,
Itworx4me

g1smd

1:09 am on Feb 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



A URL 'exists' as soon as you create a link pointing to that resource. The solution is to link to the content with the correct protocol, domain name, and filepath within that link.

Anything else involving redirects should purely be for preventing direct access to the same content via a non-canonical URL.

Note that you should list your rewrite after the redirects. This is a crucial step.

Itworx4me

1:15 am on Feb 3, 2009 (gmt 0)

10+ Year Member



Can you show me how it should look then?

Thanks,
Itworx4me

jdMorgan

2:56 am on Feb 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteEngine on
#
RewriteCond %{HTTPS} ^off$
RewriteCond %{REQUEST_METHOD} ^(GET¦HEAD)$
RewriteRule ^((grilling¦buffet)_menu\.html)$ https://www.example.com/$1 [R=301,L]
#
RewriteCond %{REQUEST_URI} !^/(grilling¦buffet)_menu\.html$
RewriteCond %{HTTPS} ^on$
RewriteCond %{REQUEST_METHOD} ^(GET¦HEAD)$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
RewriteRule ^([^/.]+)\.html$ /index.php?pageid=$1 [L]

You didn't say what the problem was, so we'll just try a few tweaks and putting the rules in the proper order first. If this doesn't work, then try substituting "%{SERVER_PORT} ^80$" for "%{HTTPS} ^off$" and "%{SERVER_PORT} ^443$" for "%{HTTPS} ^on$". And if that doesn't work, then tell us what code you tested, how you tested, what results you got, and how those results differed from those you expected.

Important: Replace the broken pipe "¦" characters above with solid pipe characters before use; Posting on this forum modifies the pipe characters.

Jim

[edit] Corrected as noted below. [/edit]

[edited by: jdMorgan at 4:08 am (utc) on Feb. 3, 2009]

Itworx4me

3:22 am on Feb 3, 2009 (gmt 0)

10+ Year Member



I get an Internal Server Error when trying both methods you described above. I am running Apache v1.3.39 if that helps.

Thanks,
Itworx4me

Itworx4me

3:23 am on Feb 3, 2009 (gmt 0)

10+ Year Member



I did fix the Pipe characters in the code.

jdMorgan

3:32 am on Feb 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What's in your server error log?

Itworx4me

3:41 am on Feb 3, 2009 (gmt 0)

10+ Year Member



Here is the errors....

[Mon Feb 2 19:25:38 2009] [alert] [client ****************] /home/****************/public_html/.htaccess: RewriteRule: unknown flag 'R301'\n
[Mon Feb 2 19:25:38 2009] [alert] [client ****************] /home/****************/public_html/.htaccess: RewriteRule: unknown flag 'R301'\n
...
[Mon Feb 2 19:15:05 2009] [alert] [client ****************] /home/****************/public_html/.htaccess: RewriteRule: unknown flag 'R301'\n
[Mon Feb 2 19:15:05 2009] [alert] [client ****************] /home/****************/public_html/.htaccess: RewriteRule: unknown flag 'R301'\n

Thanks,
Itworx4me

[edited by: jdMorgan at 4:09 am (utc) on Feb. 3, 2009]

Itworx4me

3:58 am on Feb 3, 2009 (gmt 0)

10+ Year Member



I changed the R301 to R=301 and no more Internal Errors. But now it does this when you click on the this link:
/buffet_menu.html
Changes to this link
index.php?pageid=buffet_menu
And doesn't make the link https.

Thanks,
Itworx4me

Itworx4me

4:06 am on Feb 3, 2009 (gmt 0)

10+ Year Member



Tried both methods as you described above in your first code post. Both don't work.

Thanks,
Itworx4me

jdMorgan

4:17 am on Feb 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It sounds like there is another redirect, either in another .htaccess file in a directory above and/or below this one, in your server config file(s), or perhaps within your script that is 'exposing' the internally-rewritten URL. Since neither of the two redirects in the code above would accept your script URL, they cannot be the redirects that are being invoked. And it requires an external redirect to inform the client of the URL, so your internal rewrite rule cannot be responsible for this either.

I'd be looking for a 'third party' in this situation... Make sure you don't have any redirects set up using your "control panel" that would interfere with these rules.

Jim

Itworx4me

4:27 am on Feb 3, 2009 (gmt 0)

10+ Year Member



I have checked and there are no redirects in the cp or above or below the root. I do have .htaccess files in 2 other folders but they don't deal with redirects. More of you can't access this folder or file. Any other ideas?

Thanks,
Itworx4me

g1smd

9:53 am on Feb 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



*** when you click on the this link: /buffet_menu.html ***

What happens if you change the link to https://www.yoursite.com/buffet_menu.html and click it.

Does it stay at the right location as reflected in the browser URL bar?

You'll also need Live HTTP Headers for Firefox to further diagnose the problem.

jdMorgan

3:39 pm on Feb 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, Live HTTP Headers *will* show a redirect, because there must be a redirect in order to "change the address bar" in the browser.

In addition, I did forget to remind you to completely flush your browser cache before testing any new server-side code. If you don't, your browser may continue to show you previously-cached pages and responses until the requested resource's expiry time is reached, or until the stale browser cache entry is overwritten by a newer cache entry -- either from your own site or from another.

It's possible there's an error in the code as a result of transliteration from the posts here. But it's not highly likely, and the code itself is simple and straightforward. As posted, the internal /index.php filepath cannot be revealed by a redirect from any of the rules, which is why I'm looking for an "outside agent" here.

It may be helpful to comment-out all of these rules, and then un-comment and test them one at a time.

If you continue to have problems, then feel free to re-post the code exactly as it appears on your server, changing only the domain to "example.com".

Jim

Itworx4me

12:35 am on Feb 4, 2009 (gmt 0)

10+ Year Member



Hello,

I have narrowed it down to this code.

RewriteCond %{REQUEST_URI} !^/(grilling¦buffet)_menu\.html$
RewriteCond %{HTTPS} ^on$
RewriteCond %{REQUEST_METHOD} ^(GET¦HEAD)$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

With this code being commented out the urls go to https. So something in the code above seems to be the issue. Just don't know what part of code it doesn't like.

Thanks,
Itworx4me

Thanks,
Itworx4me

Itworx4me

1:19 am on Feb 4, 2009 (gmt 0)

10+ Year Member



I think I have even narrowed it down even further. It seems to be this part of that code:
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Hope this helps,
Itworx4me

g1smd

1:22 am on Feb 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Without an associated RewriteCond, that redirect will loop forever or until the server or browser gives up.

jdMorgan

1:24 am on Feb 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I wonder if it's getting fooled by the subsequent internal rewrite to "/index.php?pageid=grilling_menu" ?

Try changing the first RewriteCond line of that code block to:

RewriteCond %{THE_REQUEST} !^[A-Z]+\ /(grilling¦buffet)_menu\.html\ HTTP/ 

once again replacing the broken pipes with solid ones.

Since THE_REQUEST is the request header sent by the client, this RewriteCond can't be fooled by a subsequent internal rewrite.

Jim

Itworx4me

1:30 am on Feb 4, 2009 (gmt 0)

10+ Year Member



Jim,

That did the trick. I want to thank you for your help with the code above. I really do appreciate it.

Thanks,
Itworx4me