doulu3399 2016-01-08 13:30
浏览 4
已采纳

Mod Rewrite 500服务器错误问题

Overview

I'm trying to rewrite:

  • http://www.foo.com/bar to the secure version https://www.foo.com/bar, then
  • https://www.foo.com/bar to https://www.foo.com/index.php?location=bar

My index.php page simply outputs the $_GET variables.

Code

.htaccess:

RewriteEngine On
RewriteBase /
RewriteRule ^https://%{SERVER_NAME}%{REQUEST_URI} [L,QSA,R=permanent]
RewriteRule ^([^/]*)$ /?location=$1 [L]

index.php:

<?php print_r($_GET); exit; ?>

Issue

  1. If I visit www.foo.com/bar, I see a 500 Server Error.
  2. If I comment out the rule for handling the $_GET "location" variable and visit foo.com, it doesn't redirect to the secure version of the site.

Update

.htaccess:

RewriteEngine on

# if https is off (it is on)
#RewriteCond %{HTTPS} off
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]

# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/?$ ?location=$1 [L,QSA]

Visiting the following results in a redirect loop error:

  • www.foo.com/bar
  • www.foo.com/index.php?location=bar
  • www.foo.com/?location=bar
  • www.foo.com?location=bar

However, if I comment out this line:

RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]

the page loads as expected:

Array ( [location] => bar )

This leads me to believe that the issue lies with the HTTPS redirect, how can I solve this?

  • 写回答

1条回答 默认 最新

  • dongshenghe1833 2016-01-08 13:33
    关注

    This rule is the problem:

    RewriteRule ^https://%{SERVER_NAME}%{REQUEST_URI} [L,QSA,R=permanent]
    

    As syntax is

    RewriteRule matching-pattern target [flags]
    

    Also you need to RewriteCond in both rules to avoid rules looping.

    You can fix it by replacing your code with:

    DirectoryIndex index.php
    RewriteEngine on
    
    # if https is off
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
    
    # If the request is not for a valid directory
    RewriteCond %{REQUEST_FILENAME} !-d
    # If the request is not for a valid file
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]*)/?$ ?location=$1 [L,QSA]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料