htaccess没有返回正确的价值

时间:2014-07-31 02:17:40

标签: php apache .htaccess mod-rewrite

我使用mod_rewrite变得疯狂。

我的htaccess文件如下:

RewriteEngine on
RewriteRule ^(.*)/(.*)/$ index.php?lang=$1&page=$2 [L]
RewriteRule ^(.*)/$ index.php?lang=$1

var_dump($_GET)上的domain/en/Home/显示

array(2)
{ 
          ["lang"]=> string(19) "redirect:/index.php" 
          ["page"]=> string(4) "Home" 
}

我不知道redirect:/index.php的来源,但我发现如果我创建一个名为en的文件夹,它可以完美地返回

array(2) 
{ 
        ["lang"]=> string(2) "en" 
        ["page"]=> string(4) "Home"
}

任何人都可以向我解释这种行为以及如何在不创建无用的空文件夹的情况下解决它吗?

感谢。

2 个答案:

答案 0 :(得分:0)

将.htaccess更改为此

RewriteEngine on
RewriteRule ^(.*)/(.*)$ index.php?lang=$1&page=$2 [QSA]
RewriteRule ^(.*)$ index.php?lang=$1 [QSA]

或者你可以更轻松地向index.php发送一个变量,就像这样

RewriteRule ^(.*)$ index.php?data=$1 [QSA]

在index.php中你的index.php需要这样做

if(isset($_GET['data']))
{
   $data = explode("/",$_GET['data']);
   $lang = $data[0];
   if(isset($data[1]))
      $page = $data[1];
}

我给你的第二个解决方案适用于所有情况,但是当你尝试访问domain/en/home/abcd时,第一个解决方案有时不起作用,在这种情况下$ _GET会返回此

$_GET['lang'] = 'en/home';
$_GET['page']= 'abcd';

答案 1 :(得分:0)

这种情况正在发生,因为您的重写规则不止一次循环。你有这样的规则:

Options +FollowSymLinks -MultiViews
RewriteEngine on

# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^([^/]+)/([^/]+)/?$ index.php?lang=$1&page=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ index.php?lang=$1 [L,QSA]

最好在这里使用[^/]+代替.*

相关问题