2

I want to use Apache's mod_rewrite to redirect all URLs that does not end with /feed to a secure URL (identical, but starting with https), and leave all that ends with /feed unaltered:

I want this:

http://example.com/node/1234 => https://example.com/node/1234
http://example.com/blog/feed => http://example.com/blog/feed

Then I added the following rules:

RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule !^/?(.*)/feed$ https://%{SERVER_NAME}/$1 [R,L]

What happens is this:

http://example.com/node/1234 => https://example.com/node/1234
http://example.com/blog/feed => https://example.com

The first one is the expected result, the second is not. Comment by w3d explains why this one doesn't work.

I have also tried (based on Martijn's answer):

RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !/feed$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

While I can't get this to work, I now think the problem is located elsewere and that this is the correct way to write this rule.

  • 2
    Why not redirect feed URLs as well? – Andrew Lott Dec 16 '15 at 9:46
  • 1
    @AndrewLott. The feed is supposed to consumed by another server (not under my control) that refuses to accept https (it seems to think the certificate is not valid). It is happy consuming http. Since I don't have direct access to this server, it has hard to debug the certificate problem. I hoped that providing it with a http-feed would be a simple solution. – Free Radical Dec 16 '15 at 10:12
  • !^/?(.*)/feed$ - you cannot have a capturing subpattern in a negated regex in mod_rewrite. $1 will always be empty. – MrWhite Dec 16 '15 at 15:29
6

You can use $ which means "Ends"

RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !/feed$
# "Ends with /feed" ------------^
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
  • 1
    Based on your updated question, I highly doubt that's because of my code, there must be another part in your htaccess doing that. To fix the double slashes, simply remove them from the htaccess where needed, it depends on some server settings – Martijn Dec 16 '15 at 15:06
  • Try taking out the slash: https://www.%{HTTP_HOST}$1 and see if that eliminates the double slash problem – Stephen Ostermiller Dec 16 '15 at 15:14
  • Thanks for the help so far! It looks I am experiencing some caching issues that have resulted in some confusion on my part. There may be "another part" that interferes. – Free Radical Dec 16 '15 at 15:52
  • Tip for FireFox: Open historybar, rightclick -> forget website removes cache and htaccess cache – Martijn Dec 16 '15 at 15:56
1

An alternative to Martijn's answer, except using a negative-lookbehind in the RewriteRule pattern without using an additional condition:

RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)(?<!/feed)$ https://example.com/$1 [R,L]

Matches all URLs that do not end with /feed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.