2015-02-10 14 views
0

解决!

首先,我知道我可以使用“?page = ...”,但我不喜欢那个变量在我的URL中。现在经过一些搜索后,我发现一个网站使用$ _GET ['page']而不在URL中显示“?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"); 
    } 
} 

我不明白这个代码是如何工作的。在我的网站中,“$ GET ['pagina']”永远不会被填满,这是合乎逻辑的。但是如何在其他网站上填写?

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

以下内容添加到您的.htaccess文件:

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

阅读'.htaccess' – AbraCadaver 2015-02-10 16:40:34

+0

正如@AbraCadaver所说,.htaccess是你想要的,但是当你在它的时候:[阅读PHP文档](http://php.net/eregi)。 'ereg'和'eregi'已被_deprecated_(并已使用多年),请使用'preg_ *'来代替 – 2015-02-10 16:41:52

+0

Wordpress可以像这样工作,你可以在那里看看。 – 2015-02-10 16:43:01

回答

1

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

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

例如,如果您现在浏览到http://yourwebsite.com/fubar,您的网站将加载位于页面的页/没有改变URL的fubar.php。

1

您可以将您的.htaccess文件中使用RewriteRule

例如:

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

这将 “改写” 像http://example.com/?pagina=even_voorstellen请求弄成http://example.com/even_voorstellen。您的index.php然后可以使用$_GET['pagina']来调用相应的页面。

enter image description here

+0

所以, 重写规则^([A-ZA-Z0-9 _-] +)/?$ \t \t \t \t \t \t的index.php?pagina = $ 1 [L] 重写规则^([A-ZA-z0- 9 _-] +)/([A-Za-z0-9 _] +)/?$ \t \t index.php?pagina = $ 2&id = $ 1 [L] 是我在找什么, :) – 2015-02-10 16:47:13

+0

是的,但我不确定URL中的id是什么。你会想在第一条规则中删除'[L]',因为这意味着'END',它会停止重写。 – 2015-02-10 16:49:03