简体   繁体   中英

htaccess rewrite conflict with https

I have the following rewrite:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)$ user_home.php?domain=$1 [L,QSA]

Which works fine for rewriting my urls, but I now need to force HTTPS,so I am using this:

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Both work, but they do not work together.

mysite.com/somepage should redirect to https://mysite.com/somepage ,

but instead it redirects to https://mysite.com/user_home.php?domain=somepage .

How can I get this to work?

This works for me; I'm guessing your problem may be on the server configuration side or somewhere else perhaps. The rewrite rules are fine though AFAICT.

<VirtualHost *:80>
    ServerAdmin webmaster@crazysite.com
    DocumentRoot /var/www
    <Directory /var/www/>
        Options FollowSymLinks
        AllowOverride All 
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>

<VirtualHost _default_:443>
    ServerAdmin webmaster@crazysite.com
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
    SSLEngine on
    SSLCertificateFile    /etc/ssl/certs/ssl-cert-snakeoil.pem
    SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
</VirtualHost>

/var/www/.htaccess

RewriteEngine On

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)$ user_home.php?domain=$1 [L,QSA]

/var/www/user_home.php

<?php
var_dump($_GET['domain']);

Loading http://crazysite.com/test redirects to https://crazysite.com/test and the output on the page is

string(4) "test"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM