Saturday, June 24, 2017

Apache mod_rewrite for specific folders and paths

Leave a Comment

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

Now this one gets really tricky as I need HTTPS on this directory, except for two cases, such as "/surf" which actually is rewritten from "surf.php", and "promote-([0-9a-zA-Z-]+)$" which is rewritten from "promote.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 have tried some stuff but which only resulted in some weird 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 everywhere in /ptp/ except /ptp/surf (which is rewritten from surf.php AND /ptp/promote-123 which is rewritten from promote.php?user=123

Currently I'm using PHP to redirect to HTTP or HTTPS as per my needs but I know that it would be much faster if I could manage to do it via rewrites. Any pointers, tips, suggestions? Thanks.

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, the resources such as javascript, fonts etc, were being blocked by the browser, unless I specified absolute HTTPS paths. Note that this never happened when redirecting through PHP...

2 Answers

Answers 1

I changed a little bit and it works perfectly

Changes

Remove the Change the RewriteRule to match file to .php to bottom.

Remove the $ sign that is End of the pattern

As Said in the update promote-1111 will redirect to promote.php?user=$1 change the promote-[0-9]+ to promote(-[0-9]+)? otherwise it will override in the second redirection as you redirecting it to promote.php?user=$1

The Code

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

The page surf

enter image description here

The Page Index

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

Will work with servers

enter image description here

Answers 2

Your rules aren't in the "update" working because of side effects of using <Directory> context. Each substitution starts processing again.

When you request /promote-123 and rewrite it to put the numbers in the query string, you can't then match the numbers as if they're still in the path. You'll need to match the rewriten path and the numbers with RewriteCond %{QUERY_STRING} (if you care about the numbers)

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment