开发者

.htaccess url rewrite with ssl redirection

I'm having trouble combining a url query parameter rewrite (fancy-url) with a .htaccess ssl redirection.

My .htaccess file is currently:

Options +FollowSymLinks
Options -Indexes
ServerSignature Off
RewriteEngine on
RewriteBase /

# in https: process secure.html in https
RewriteCond %{server_port} =443
RewriteCond $1 ^secure$ [NC]
RewriteRule ^(.+).html$ index.php?page=$1 [QSA,L]

# in https: force all other pages to http
RewriteCond %{server_port} =443
RewriteCond $1 !^secure$ [NC]
RewriteRule ^(.+).html$ http://%{HTTP_HOST}%{REQUEST_URI} [QSA,N]

# in http: force secure.html to https
RewriteCond %{server_port} !=443
RewriteCond $1 ^secure$ [NC]
RewriteRule ^(.+).html$ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,N]

# in http: process other pages as http
RewriteCond %{server_port} !=443
RewriteCond $1 !^secure$ [NC]
RewriteRule ^(.+).html$ index.php?page=$1 [QSA,L]

The fancy-url rewriting is working fine but the redirection to/from https isn't working at all.

If I replace the 2 lines containing

RewriteRule ^(.+).html$ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,N]

with

RewriteRule ^(.+).html$ https://%{HTTP_HOST}/index.php?page=$1 [QSA,L]

then the https redirection works fine but the fancy-url rewriting doesn't work.

Is it possible to combine these two?

Edit:

The desired results are:

1. http://domain.com/secure.html is rewritten to https://domain.com/index.php?page=secure
2. http://domain.com/foo.html is rewritten to http://domain.com/index.php?page=foo
3. https://domain.com/secure.html is rewritten to https://domain.com/index.php?page=secure
4. https://domain.com/foo.html is rewritten to http://domain.com/index.php?page=foo

(I had to put them in a code block as more than 1 link is not allowed for new users)

So secure.html is always https while foo.html (all other pages) are always http.

Solution:

Thanks to Gumbo, the solution is:

Options +FollowSymLinks
Options -Indexes
ServerSignature Off
RewriteEngine on
RewriteBase 开发者_JAVA技巧/

# in https: force all other pages to http
RewriteCond %{server_port} =443
RewriteCond $1 !^secure$ [NC]
RewriteRule ^(.+)\.html$ http://%{HTTP_HOST}%{REQUEST_URI} [QSA,N]

# in http: force secure.html to https
RewriteCond %{server_port} !=443
RewriteCond $1 ^secure$ [NC]
RewriteRule ^(.+)\.html$ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,N]

# in https: process secure.html in https
RewriteCond %{server_port} =443
RewriteCond $1 ^secure$ [NC]
RewriteRule ^(.+)\.html$ index.php?page=$1 [QSA,L]

# in http: process other pages as http
RewriteCond %{server_port} !=443
RewriteCond $1 !^secure$ [NC]
RewriteRule ^(.+)\.html$ index.php?page=$1 [QSA,L]


You need to put those rule, that cause an external redirect, before those rules, that just cause an internal redirect. So:

# in https: force all other pages to http
RewriteCond %{SERVER_PORT} =443
RewriteCond $1 !^secure$ [NC]
RewriteRule ^(.+)\.html$ http://%{HTTP_HOST}%{REQUEST_URI} [QSA,L]

# in http: force secure.html to https
RewriteCond %{SERVER_PORT} !=443
RewriteCond $1 ^secure$ [NC]
RewriteRule ^(.+)\.html$ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,L]

# in https: process secure.html in https
RewriteCond %{SERVER_PORT} =443
RewriteCond $1 ^secure$ [NC]
RewriteRule ^(.+)\.html$ index.php?page=$1 [QSA,L]

# in http: process other pages as http
RewriteCond %{SERVER_PORT} !=443
RewriteCond $1 !^secure$ [NC]
RewriteRule ^(.+)\.html$ index.php?page=$1 [QSA,L]


That is not the correct way to detect what the server port is, it should follow the same rules as http_host using ^443$ and !^443 respectively. You really should capitalize server_port as well, it's good practice. Here is a good little tutorial that might help you out.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜