Home Exclude a URL from mod_rewrite rule - stop redirect to home page
Reply: 1

Exclude a URL from mod_rewrite rule - stop redirect to home page

Sam Published in 2018-01-06 10:57:21Z

I need a mod_rewrite to redirect all http requests to https, but I want do exclude one URL

Here's what I have so far in .htaccess file.

# force https
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/template/.*$ [NC]
RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,QSA]

# 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]
# END WordPress

http://example.com/template/` should not be redirected. But it is being redirected to the home page.

301 Moved Permanently
301 Moved Permanently
200 OK

Can anyone figure out why it's causing a redirect to the home page?

Thank you!

Ben Reply to 2018-01-07 17:27:17Z
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !^[A-Z]+\ /template/.*\ HTTP [NC]
RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,QSA]

The .htaccess is not good. The first request http://example.com/template/ matches the last RewriteRule, which is internal rewritten to http://example.com/index.php which matches the first RewriteRule and redirect to https://example.com/index.php permanently. The REQUEST_URI is rewritten internally due to the previous RewriteRule referencing it. The THE_REQUEST is the exact request header as originally received from the client. It is not updated by any internal rewrites.


The path component of the requested URI, such as "/index.html". This notably excludes the query string which is available as its own variable named QUERY_STRING.


The full HTTP request line sent by the browser to the server (e.g., "GET /index.html HTTP/1.1"). This does not include any additional headers sent by the browser. This value has not been unescaped (decoded), unlike most other variables below.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.307323 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO