Mod Rewrite

I have the following example of code in my apache configuration file for a virtual host:

RewriteEngine on
RewriteCond %{SERVER_PORT} !443
RewriteCond %{REQUEST_FILENAME} !^/test\.php$
RewriteRule   ^(.*)$  https://mywebsite.com$1  [R]

This works fine for this file.  However, now i have 2 files, and a directory which I want to be accessible via http, and force the rest to be accessible via https.

The following i want to allow http access to are:

/test.php
/dev/test.php
/xsl

thanks

LVL 1
axman505Asked:
Who is Participating?
 
caterham_wwwCommented:
This is a logical AND:

match every request (RegEx RewriteRule) --> server port is not 443 and --> REQUEST_URI is not /test.php, /dev/test.php and --> REQUEST_URI does not start with /xsl/ ----> 302 redirect https:....
0
 
caterham_wwwCommented:
Just adjust/add a condition:

RewriteEngine on
RewriteCond %{SERVER_PORT} !=443
RewriteCond %{REQUEST_URI} !^/(dev/)?test\.php$
RewriteCond %{REQUEST_URI} !^/xsl/
RewriteRule   ^(.*)$  https://mywebsite.com$1  [R,L]

Better use REQUEST_URI, REQUEST_FILENAME might contain sometimes not the same value of REQUEST_URI (that is the case when an uri-to-filename translation already took place).
0
 
axman505Author Commented:
I always thought you needed to have [OR] after rules.  Isnt how this is requiring all to be matched?  Wouldn't it need to be:

RewriteEngine on
RewriteCond %{SERVER_PORT} !=443
RewriteCond %{REQUEST_URI} !^/(dev/)?test\.php$ [OR]
RewriteCond %{REQUEST_URI} !^/xsl/
RewriteRule   ^(.*)$  https://mywebsite.com$1  [R,L]


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.