$ _GET导航没有使用?page =

时间:2015-02-10 16:39:31

标签: php html

解决!

首先,我知道我可以使用"?page = ..."但我不喜欢我的网址中的那个变量。现在经过一些搜索,我找到了一个使用$ _GET ['页面']的网站,但没有显示"?page ="在网址中。

但是我对代码有点挣扎。我似乎无法使此代码工作。我知道它适用于其他网站,但不适用于我的网站。我似乎无法找到它在其他网站上的工作原理。

这是菜单代码:

<li><a href="index.php">Home</a></li>
<li><a href="even_voorstellen">Even voorstellen</a></li>
<li><a href="de_kennismaking">De kennismaking</a></li>

,这是php代码:

$sExpressie = "(http:|ftp:|shttp:|www.|.php|.pl|.cgi|.asp|index.php)";  

if(isset($_GET['pagina']) && eregi($sExpressie,$_GET['pagina']))
{
   include("pages/home.php");
}
else 
{
   if(isset($_GET['pagina']) && file_exists('Pages/' . $_GET['pagina'] . ".php")) 
   {                                
     include('pages/' . $_GET['pagina'] . ".php");
   }                    
   else 
   { 
     include("pages/home.php");
   } 
}

我不明白这段代码是如何运作的。在我的网站上,&#34; $ GET [&#39; pagina&#39;]&#34;永远不会被填补,这是合乎逻辑的。但是如何在其他网站上填写?

非常感谢大家! 解决方案:

将以下内容添加到.htaccess文件中:

RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/?$ pages/$1.php [NC,L]

2 个答案:

答案 0 :(得分:1)

您可以在.htaccess文件中使用RewriteRule

例如:

RewriteEngine On
RewriteRule ^/(.*)$ index.php?pagina=$1 [L, QSA] #(.*) is lazy and you may want to just catch [a-z0-9].

这将&#34;重写&#34;像http://example.com/?pagina=even_voorstellen这样的请求http://example.com/even_voorstellen。然后,您的index.php可以使用$_GET['pagina']来呼叫相应的网页。

enter image description here

答案 1 :(得分:1)

您需要将以下内容添加到.htaccess文件中:

RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/?$ pages/$1.php [NC,L]

例如,如果您现在导航到http://yourwebsite.com/fubar,您的网站将加载位于pages / fubar.php的页面而不更改网址。