user2736203
user2736203

Reputation: 195

.htaccess - 301 redirect move all pages to https:// - except 1 page?

I have the following code that redirects any http:// request to https:// - This work great, but how would I edit this to make an exception for one page e.g. mydomain.com/sitemap-news.xml - and keep this as http:// ?

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [L,R=301]

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

As listed in comments here is how my entrie .htaccess looks

# BEGIN WordPress
  <IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress


RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule !^sitemap-news\.xml$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

RewriteCond %{HTTPS} off
RewriteRule !^sitemap-news\.xml$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE,NE]

Upvotes: 1

Views: 521

Answers (1)

anubhava
anubhava

Reputation: 785108

You can make an exception for sitemap file:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule !^sitemap-news\.xml$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

RewriteCond %{HTTPS} off
RewriteRule !^sitemap-news\.xml$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE,NE]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Upvotes: 1

Related Questions