Apache mod_rewrite for specific folders and paths

I found dozens of articles online on how to set up mod_rewrites, but for the love of God, I can't figure out how to PROPERLY force HTTPS on ALL pages and then force HTTP on certain directories or (already rewritten) pages.

Now it gets really tricky as I need HTTPS in this directory except for two cases like "/ surf" which are actually rewritten with "surf.php" and "promotion - ([0-9a-zA - Z -] +) $ ", which is rewritten from the word" promotion.php? User = $ 1 ":

<Directory /home/rotate/public_html/ptp/>
    AllowOverride None
    Order Deny,Allow
    Allow from all

Options +SymLinksIfOwnerMatch
ErrorDocument 404 "<h1>Oops! Couldn't find that page.</h1>"
RewriteEngine On
RewriteRule ^promote-([0-9]+)$ promote.php?user=$1 [QSA,NC,L]
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]

</Directory>

      

I've tried some things but only resulted in some strange redirection loops ...

RewriteCond %{HTTPS} on
RewriteRule !^(surf|promote-([0-9]+)$) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

      

So basically I need to force HTTPS all over the place in / ptp / except / ptp / surf (which is rewritten from surf.php AND / ptp / promotion-123, which is rewritten from promotion.php? User = 123

I am currently using PHP to redirect to HTTP or HTTPS as per my needs, but I know it will be much faster if I can do it via rewriting. Any pointers, hints, suggestions? Thank.

UPDATE2: This worked:

RewriteCond %{HTTPS} off
RewriteRule !^(surf|promote(-[0-9]+)?) https://%{HTTP_HOST}%{REQUEST_URI} [R=301]

RewriteRule ^promote-([0-9]+)$ promote.php?user=$1 [NC,L]
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php

      

However, resources like javascript, fonts, etc. were blocked by the browser unless I specified the absolute HTTPS paths. Note that this never happened when redirecting via PHP ...

+3


source to share


2 answers


I changed a bit and it works fine

Changes

Remove Edit RewriteRule

to map the file to the .php

bottom.

Delete the mark $

that is End of Figure

As said in update promote-1111

will redirect to promote.php?user=$1

change promote-[0-9]+

to promote(-[0-9]+)?

, otherwise it will be overridden in the second redirect when redirecting it topromote.php?user=$1

Code

RewriteCond %{HTTPS} off
RewriteRule !^(surf|promote(-[0-9]+)?) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]

      



Surf page

enter image description here

Page index

Ignore the error message shown in this image. Since I tried this from my localhost it won't have a certificate.

Will work with servers

enter image description here

+2


source


Your rules do not work in "update" because of the side effects of using context <Directory>

. Each substitution starts processing again.



When you query / advertise -123 and rewrite it to put the numbers in the query string, you won't be able to match the numbers as if they were still on the way. You will need to match the rewrite path and numbers with RewriteCond% {QUERY_STRING} (if you need numbers)

+1


source







All Articles