htaccess

时间:2016-01-12 14:32:14

标签: php apache .htaccess mod-rewrite redirect

大家好,这是我的htaccess,我有我的https我想将所有非https重定向到https而不影响我的get / post变量和任何其他链接只是简单地将网站移动到https

htaccess文件

   RewriteEngine On
   RewriteBase /
   Options All -Indexes
   RewriteCond %{REQUEST_METHOD} POST [NC]
   RewriteRule ^ - [L]
   ErrorDocument 403 /www.domain.com/error404.php
   RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.*$ [NC] 


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

   # redirect /profile?eid=1 to /eid/1
   RewriteCond %{THE_REQUEST} \s/+profile(?:\.php)?\?(eid)=(\d+)\s [NC]
   RewriteRule ^ /%1/%2? [R=301,L]

   # internally rewrite /eid/1 to /profile.php?eid=1
   RewriteRule ^(eid)/(\d+)$ profile.php?$1=$2 [L,QSA,NC]



   # redirect /dept?did=1 to /did/1
   RewriteCond %{THE_REQUEST} \s/+dept(?:\.php)?\?(did)=(\d+)\s [NC]
   RewriteRule ^ /%1/%2? [R=301,L]

   # internally rewrite /did/1 to /dept.php?did=1
   RewriteRule ^(did)/(\d+)$ dept.php?$1=$2 [L,QSA,NC]

  RewriteCond %{THE_REQUEST} \s/+enterprise\.php\?url=([^\s&]+) [NC]
  RewriteRule ^ /%1? [R=301,L]

  RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
  RewriteRule ^ /%1 [R=302,L,NE]

   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
   RewriteRule ^(.+?)/?$ $1.php [L]

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

我尝试过将代码添加到我的htaccess文件

  RewriteCond %{HTTPS} !on
  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

但它将网站重定向到错误页面,但是以https模式

我在htaccess页面中添加了403错误处理程序之后的代码..

我在htaccess中不太好请告诉我一些事情.. 只需在首次尝试时将网站重定向到https而不是http

1 个答案:

答案 0 :(得分:4)

您应该使用redirect告诉R flag并提供状态code,这将有助于您的搜索引擎优化。将此规则作为您的第一条规则

   Options All -Indexes
   RewriteEngine On
   RewriteBase /
   RewriteCond %{HTTPS} !on
   RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
   #all other rules below