Jon Zangitu
Jon Zangitu

Reputation: 967

.htaccess rewrite except some request_uri

I have a .htaccess file but it's not working as expected.

Redirect /some/dir http://www.example.com/someother/dir/29

<IfModule mod_rewrite.c>
  RewriteEngine on

  # HTTPS
  RewriteCond %{HTTPS} off
  RewriteCond %{SERVER_PORT} 80
  RewriteCond %{REQUEST_URI} !=/buy/confirm
  RewriteCond %{REQUEST_URI} !=/index.php
  RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

  # Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteCond %{REQUEST_URI} !=/some/dir
  RewriteRule ^(.*)$ index.php?issue=$1 [L,QSA]
</IfModule>

I've been checking this with http://htaccess.mwl.be/ and I don't understand a line when I enter "http://www.example.com/buy/confirm":

RewriteCond %{REQUEST_URI} !=/buy/confirm   This condition was met.

What exactly contains %{REQUEST_URI} to met the condition != /buy/confirm?? I've tried both /buy/confirm and buy/confirm and nothing.

I don't want to redirect to HTTPS if the url is buy/confirm.

Thanks!

Upvotes: 1

Views: 2693

Answers (2)

Croises
Croises

Reputation: 18671

You can use:

Redirect /some/dir http://www.example.com/someother/dir/29

<IfModule mod_rewrite.c>
  RewriteEngine on

  # HTTPS
  RewriteCond %{HTTPS} off
  RewriteCond %{REQUEST_URI} !^/buy/confirm
  RewriteCond %{REQUEST_URI} !^/index.php
  RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

  # Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !/favicon.ico
  RewriteCond %{REQUEST_URI} !^/some/dir
  RewriteRule ^(.*)$ index.php?issue=$1 [L,QSA]
</IfModule>

Upvotes: 1

Leśniakiewicz
Leśniakiewicz

Reputation: 904

Try this:

RewriteEngine On

#redirect everything except /buy/confirm to https
RewriteCond %{HTTPS} =off
RewriteCond %{REQUEST_URI} !^\/buy\/confirm\/
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]    

#redirect /buy/confirm to http if it is accesed by https
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} \/buy\/confirm\/
RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]

Upvotes: 0

Related Questions