Forum Moderators: phranque

Message Too Old, No Replies

Rewrite Rules Created New Problem

forced / at end of directory

         

jackhandy

7:21 am on Aug 30, 2006 (gmt 0)

10+ Year Member



I have an SSL certificate installed on my web site for domain.net (no www). The site is not in httpd.conf as a port 443 (https) site but rather will come up on port 80 (http), so it is listed as both. This is on a hosting reseller account with Cpanel and I don't have root access to the server. There are only two directories on domain.net that require https on the site and those directories require a trailing slash to force https under some circumstances.

Here is what I put in the .htaccess located in the domain account root directory (ie, public_html) to force no www, to force a trailing slash on the two directories that require it (but includes all directories), and to force https on the two directories:

------------------------
RewriteEngine On
RewriteBase /

#No .www in domain name
RewriteCond %{HTTP_HOST}!^domain.net$ [NC]
RewriteRule ^(.*)$ [domain.net...] [L,R=301]

#Force trailing / on all directories
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_URI}!(.*)/$
RewriteRule ^(.*)$ [domain.net...] [L,R=301]

#Force https on 'directory1' and 'directory2' directories
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} directory1 [OR]
RewriteCond %{REQUEST_URI} directory2
RewriteRule ^(.*)$ [domain.net...] [R,L]
------------------------

The circumstances that caused me to do this is people getting 403 on www.domain.net/directory1, domain.net/directory1, www.domain.net/directory2, and domain.net/directory2 with a forced https rewrite in directory1 and 2. The forced https in directories 1 & 2 worked only if they included a trailing / after the directory or went as far as to include index.html on the URL (except directory1 is all PHP). Not all people can figure that out and this is a money making site. I researched it and the above is what I came up with that works to overcome human error. The only problem that resulted is that now any [subdomain.domain.net...] comes up domain.net/subdomain in a browser. I can't find any rewrite rule that will fix the subdomain issue while keeping the above intact. Can someone help?

Caterham

12:20 pm on Aug 30, 2006 (gmt 0)

10+ Year Member



The only problem that resulted is that now any [subdomain.domain.net...] comes up domain.net/subdomain in a browser
That might be caused by yourn first rule, because your cond. is also true for a subdomain.

RewriteEngine On
#No .www in domain name
# the other way, host is www.domain.net
RewriteCond %{HTTP_HOST} =www.domain.net
RewriteRule ^(.*)$ http://domain.net/$1 [L,R=301]

#Force trailing / on all directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]

#Force https on 'directory1' and 'directory2' directories
RewriteCond %{SERVER_PORT} =80
RewriteRule ^((directory1¦dir2).*)$ https://domain.net/$1 [R,L]

[edited by: Caterham at 12:21 pm (utc) on Aug. 30, 2006]

jackhandy

9:46 pm on Sep 3, 2006 (gmt 0)

10+ Year Member



This doesn't work, shouldn't there be an [NC] or something on the condition?

RewriteEngine On
#No .www in domain name
# the other way, host is www.domain.net
RewriteCond %{HTTP_HOST} =www.domain.net
RewriteRule ^(.*)$ [domain.net...] [L,R=301]

However, this does work and it returned my subdomains back to subdomain.domain.com. (I got this one from [no-org...]

RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ [domain.com...] [R=301,L]

Thanks anyway for the assist!

jdMorgan

10:11 pm on Sep 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you test this rule with "www.domain.com:80/" using a Mozilla browser, it will fail. I suggest you use either:

RewriteCond %{HTTP_HOST} ^www\.domain\.c[b]om[/b] [NC]
RewriteRule (.*) http://domain.com/$1 [R=301,L]

-or-

RewriteCond %{HTTP_HOST} ^www\.domain\.com[b](:80)?[/b]$ [NC]
RewriteRule (.*) http://domain.com/$1 [R=301,L]

-or-

RewriteCond %{HTTP_HOST} ^www\.domain\.com[b](:[0-9]{1,5})?[/b]$ [NC]
RewriteRule (.*) http://domain.com/$1 [R=301,L]

In other words, either remove the end-anchor from the HTTP_HOST pattern, or add the regex needed to match the appended port number.

Jim