2014-02-19 108 views
0

我想通过url传递变量,而不包括?var1 = test,但用/ test取代了。我有以下代码:通过使用斜杠网址传递参数

$pathinfo = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : $_SERVER['REDIRECT_URL'];  
$params = preg_split('|/|', $pathinfo, -1, PREG_SPLIT_NO_EMPTY); 
print_r($params); 

这个问题是,如果我把它放在index.php文件中,我必须包括文件名。

我得到什么: http://example.com/index.php/test

我想要什么: http://example.com/test

我该怎么办?

+0

这被称为URL重写。看看它。 http://stackoverflow.com/questions/16388959/url-rewriting-with-php –

回答

2

在你的根文件夹中添加一个.htaccess文件并粘贴此代码

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /index.php?/$1 [L] 

只要确保mod_rewrite的启用

0

What should I do?

使用使用mod_rewrite.htaccess重写规则。您的规则可能看起来像:

(例如,从WordPress的):

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f # Existing File 
RewriteCond %{REQUEST_FILENAME} !-d # Existing Directory 
RewriteRule . /index.php [L] 

更多的例子here和一些更 “简单” 的解释here

0

尝试在您对此内容的根目录创建一个名为.htaccess文件:

<IfModule mod_rewrite.c> 
    RewriteEngine On 

    # Redirect Trailing Slashes... 
    RewriteRule ^(.*)/$ /$1 [L,R=301] 

    # Handle Front Controller... 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule^index.php [L] 
</IfModule> 

还要确保modrewrite已启用。