将HTTP请求重定向到HTTPS会干扰我的seo友好URL

时间:2016-02-17 20:56:59

标签: apache .htaccess mod-rewrite rewrite

我正在使用.htaccess创建seo友好网址,并将我的HTTP请求重定向到HTTPS

当我不使用HTTP到HTTPS规则时,我的网址会读取 即http://www.domain.com,当有人点击用户名时,网址会更改为http://www.domain.com/username

但是当我使用HTTP到HTTPS规则时,当我点击用户名时它会干扰网址 即https://www.domain.com,当有人点击用户名时,网址会更改为https://www.domain.com/username?d=username

当我使用HTTP到HTTPS规则时,我会想要它,并且我点击用户名我的网址看起来像 即https://www.domain.com,当有人点击用户名时,网址会更改为https://www.domain.com/username

问题是https://www.domain.com/username?d=username网址

这是我的.htaccess代码

RewriteEngine On

RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?d=$1


#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

1 个答案:

答案 0 :(得分:4)

首先放置重定向并使用L标志。如果您使用域名,也可以合并规则。

RewriteEngine On

#redirect to https and/or www/https
RewriteCond %{HTTP_HOST} ^domain\.com [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]

RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?d=$1 [L]