如何更改网址而不更改文件夹?

时间:2012-12-21 04:53:42

标签: php url-routing smarty

我在我的网站上使用MVC框架。在我的网页中没有显示URL文件扩展名。我需要指定文件扩展名,如

www.legacy.com/women/tops.php 

但仅显示

www.legacy.com/women/tops/ 

对于seo目的我想更改带扩展名的网址。但我无法更改文件夹或文件名。

我还有一个疑问。我的网页正在显示

www.legacy.com/women/tops/ 

像这样我想改变这个www.legacy.com/women_tops.php/ 可能吗?

谢谢

以下是我用来将URL与控制器关联的代码:

$arrCommands = array (
    'home' => "contents",
    'members' => "",        
);

if ( $arrCommands[$command1] == "*" )
{
    $includeFile = CONTROLLER_PATH . "/" . $command1 . "/" . $command2 . ".php";

    if ( !file_exists($includeFile) )
        $includeFile = CONTROLLER_PATH . "/" . $command1 . "/default.php";
}
    elseif ( !array_key_exists($command1, $arrCommands) )
    {
        $includeFile    =   CONTROLLER_PATH . "/contents/" . $command1 . ".php";
        if ( !file_exists($includeFile))
    Header( "Location: http://metroplots.ragedev/404_error/" );     
    }
else
{
    $includeFile = CONTROLLER_PATH . "/". $arrCommands[$command1] . "/" . $command1 . ".php";

    if ( !file_exists($includeFile) )
        $includeFile = CONTROLLER_PATH . "/contents/home.php";
}

include_once($includeFile);

7 个答案:

答案 0 :(得分:5)

.htaccess可以做魔法。可以重写任何你想要的东西,可能会弄乱... http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

您还应该能够在应用程序本身中执行此操作。在大多数优秀的框架中,您可以使用自定义路由here is zend example

答案 1 :(得分:2)

您所寻找的技术称为网址重写

这个想法是在给服务器端并在服务器端重写它。许多软件使用此方法不通过get参数公开逻辑......

e.g。 http://domain.com/blog/2

根据服务器,这个网址实际上是:

http://domain.com/index.php?cat=blog&page=2

Linux / apache服务器上的

是通过modrewrite实现的: http://httpd.apache.org/docs/current/mod/mod_rewrite.html

在服务器上使用.htaccess文件来解释重写规则和路由URL。

microsoft iis服务器有自己的风格(和语法),称为url rewrite: http://www.iis.net/downloads/microsoft/url-rewrite

有一些工具可以帮到你(这里有6个概述) http://webm.ag/2009/12/15/6-of-the-best-mod-rewrite-generators/

但我觉得大多数时候你最好的是手动创建自己的。

这是我在我的网站上使用的一个例子:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

基本上这个规则集会将所有流量重定向到index.php

在我的MVC中我有一个名为“url_logic”的通用控制器,它首先运行并查看url是什么。并根据它的逻辑创建必要的控制器来创建站点(即使它是一个404错误控制器)。

希望有助于您入门!

另外,如果您使用Windows .htaccess文件很难处理。我建议命名他们 htaccess.txt,当你将它们上传到服务器时,将它们重命名为。

答案 2 :(得分:1)

您必须在htaccess中添加以下2行

RewriteEngine On

RewriteRule ^women/tops /women_tops.php

答案 3 :(得分:1)

创建一个index.php文件,该文件将被读取为显示的默认文件并将其重定向到您的文件。

答案 4 :(得分:1)

你正在寻找的是mod_rewrite mod_rewrite的:

在网址中隐藏扩展名:

mod_rewrite重写规则生成器将获取一个给它的动态URL,并生成正确的语法放在.htaccess文件中,以允许以spiderable格式重写url。 apache模块mod_rewrite将特定格式的URL转换为另一种格式,对于帮助对具有动态内容的站点编制索引非常有用。

注意:在mod_rewrite投入使用之前,您应该在apache服务器中启用它。

Syntax: RewriteRule url-pattern url-new [[flag,...]]
Example: RewriteRule ^/foo/(.*)$ /bar/$1 [R,L]

一些有用的链接 -

网址:http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html

网址:http://tomclegg.net/rewriterule

答案 5 :(得分:0)

查看this网页。我认为它可能有你的答案。

答案 6 :(得分:0)

感谢所有重播此问题的人。

$xepWords = explode('_',$command1);
    if($xepWords[0] == 'women')
    {
        $includeFile = CONTROLLER_PATH .'/women/tops.php';
    }  

我在控制器操作中添加了此代码现在它工作正常