我正在一个共享的Apache Web服务器上的PHP。我可以编辑.htaccess文件。
I'm running PHP on a shared Apache web server. I can edit the .htaccess file.
我试图模拟一个文件,文件结构,是不是真正存在。例如,我想的网址: www.Stackoverflow.com/jimwiggly
实际显示 www.StackOverflow.com/index.php?name = jimwiggly
我有一半通过编辑我的.htaccess文件按在这篇文章中说明:http://stackoverflow.com/questions/1987743/php-serve-pages-without-php-files-in-file-structure:
I'm trying to simulate a file file structure that is not actually there. For example, I would like for the URL: www.Stackoverflow.com/jimwiggly
to actually display www.StackOverflow.com/index.php?name=jimwiggly
I got halfway there by editing my .htaccess file as per the instructions in this post: http://stackoverflow.com/questions/1987743/php-serve-pages-without-php-files-in-file-structure:
RewriteEngine on
RewriteRule ^jimwiggly$ index.php?name=jimwiggly
这很好地工作,只要在地址栏仍显示 www.Stackoverflow.com/jimwiggly
和正确的页面加载,但是,我所有的相对链接保持不变。我能回到过去,并插入<?PHP的echo $ _ GET ['名称'];?>
的每一个环节之前,但似乎有可能是一个更好的办法比起那个来说。此外,我怀疑我的整个方法可能会关闭,我应该会对此有所不同?
This works nicely insofar as the URL bar still displays www.Stackoverflow.com/jimwiggly
and the correct page loads, however, all of my relative links remain unchanged. I could go back in and insert <?php echo $_GET['name'];?>
before each link, but it seems like there might be a better way than that. Additionally, I suspect my whole approach might be off, should I be going about this differently?
我觉得要做到这一点的最好办法是采用MVC样式的URL操作的URI,而不是PARAMS。
I think the best way to do this is to adopt the MVC style url manipulation with the URI and not the params.
在喜欢你htaccess的使用:
In your htaccess use like:
<IfModule mod_rewrite.c>
RewriteEngine On
#Rewrite the URI if there is no file or folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
然后在你的PHP脚本,你想开发一个小班读取URI,并把它分割成段,如
Then in your PHP Script you want to develop a small class to read the URI and split it into segments such as
class URI
{
var $uri;
var $segments = array();
function __construct()
{
$this->uri = $_SERVER['REQUEST_URI'];
$this->segments = explode('/',$this->uri);
}
function getSegment($id,$default = false)
{
$id = (int)($id - 1); //if you type 1 then it needs to be 0 as arrays are zerobased
return isset($this->segments[$id]) ? $this->segments[$id] : $default;
}
}
使用像
http://mysite.com/posts/ 22 /罗伯特 - 皮特 - 显示-MVC式-URI访问
$Uri = new URI();
echo $Uri->getSegment(1); //Would return 'posts'
echo $Uri->getSegment(2); //Would return '22';
echo $Uri->getSegment(3); //Would return 'robert-pitt-shows-mvc-style-uri-access'
echo $Uri->getSegment(4); //Would return a boolean of false
echo $Uri->getSegment(5,'fallback if not set'); //Would return 'fallback if not set'
现在在MVC中通常有像 http://site.com/controller/method/param但在非MVC风格的应用程序,你可以做 http://site.com/action/sub-动作/参数
Now in MVC There usually like http://site.com/controller/method/param but in a non MVC Style application you can do http://site.com/action/sub-action/param
希望这有助于你期待与您的应用程序移动。
Hope this helps you move forward with your application.