2016-12-22 58 views
0

我工作在本地服务器上上html/php应用程序,我想使用Apache URL重写模块没有成功的.htaccess URL重写工作,但不是重定向

应用程序存储在./Compta/index.php。我有一个.htaccess文件./Compta/.htaccess

我想只使用一个重写URL,如:http://localhost/Compta/Test/

代替:http://localhost/Compta/index.php?page=test

和重定向用户,如果他们试图去旧的URL

.htaccess文件包含:

Options +FollowSymlinks 

RewriteEngine on 

RewriteRule ^([[:alnum:]]*)/$ /Compta/index.php?page=$1 [L] 

RewriteRule ^([[:alnum:]]*)$ /Compta/index.php?page=$1 [L] 

RewriteCond %{REQUEST_URI} ^/index\.php$ 
RewriteCond %{QUERY_STRING} ^page=([[:alnum:]]*)$ 
RewriteRule ^(.*)$ http://localhost/Compta/%1/ [L,R=301] 

当我去Ť Øhttp://localhost/Compta/Test/下面一行是工作,我的代码在一个div包括test.php内容:

RewriteRule ^([[:alnum:]]*)/$ /Compta/index.php?page=$1 [L] 

当我去http://localhost/Compta/Test以下行工作,但在Firefox的URL改写为http://localhost/Compta/index.php?page=Test,这是不会发生与http://localhost/Compta/Test2;网址不会被重写。

RewriteRule ^([[:alnum:]]*)$ /Compta/index.php?page=$1 [L] 

要解决这个问题,并重定向旧的URL我添加这些行:

RewriteCond %{REQUEST_URI} ^/index\.php$ 
RewriteCond %{QUERY_STRING} ^page=([[:alnum:]]*)$ 
RewriteRule ^(.*)$ http://localhost/Compta/%1/ [L,R=301] 

但这不工作,当我去http://localhost/Compta/index.php?page=Test的网址不被改写,以http://localhost/Compta/Test/

预先感谢您

回答

0

我没有找到与.htaccess的解决方案,但我发现一个与PHP,所以我在我的PHP文件的顶部添加了以下几行:

 if(preg_match("/\/Compta\/index\.php\?page=([[:alnum:]]*)/",@$_SERVER['REQUEST_URI'],$page)&&!empty($page[1])) 
     { 
      $new_url = "/Compta/"; 
      switch (strtolower($page[1])) { 
       case "test": 
        $new_url = $new_url."Test/"; 
        break; 
       case "test2": 
        $new_url = $new_url."Test2/"; 
        break; 
       default:break; 
      } 
      header("Status: 301 Moved Permanently", false, 301); 
      header("Location: ".$new_url); 
      exit; 
     } 

我测试,如果URL以“/Compta/index.php?page=”开始,如果有一个参数“页” 然后我初始化包含新的URL 切换变量我的参数的内容我修改新的网址 ,然后我重新定向到我的新网址:)