【问题标题】:MVC pattern - Show only controller name in URL (hide index.php and action)MVC 模式 - 在 URL 中仅显示控制器名称(隐藏 index.php 和操作)
【发布时间】:2019-01-06 01:37:06
【问题描述】:

我正在使用Aruba 提供的托管 Linux 服务来运行我的网站。

此时,我的网站 URL 设计如下:

mysite.it/index.php?controller=&action=

我想隐藏index.phpaction 参数,所以我的uri 看起来像:

mysite.it/controller

我的基本文件夹结构是:

www.mysite.it/
  config/
  controller/
  lib/
  model/
  template/
  .htaccess
  index.php    

是否有解决方案来实现这一目标?

另外,我需要保留原来的document.URL,因为例如在某些.js 中有一些函数会在URL 中搜索action 参数。

提前致谢。

【问题讨论】:

标签: php .htaccess url model-view-controller


【解决方案1】:

您可以在 .htaccess 文件中包含以下文本
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]

请记住,重写引擎必须在您的 Web 服务器配置中处于活动状态。

【讨论】:

  • 我试过了,但基本上它似乎什么都没有改变......重写模块处于活动状态
  • 您是否检查过您网站所在目录的网络服务器中的配置是否允许覆盖?
  • 好的,您的 MVC 调用控制器不是作为路径的一部分而是作为参数。所以我认为你可以尝试以这种方式为每个控制器编写一个 RewriteRule:RewriteRule ^(.*)$ index.php?controller=$1&action= [L]
  • 但是查询字符串是这样保存的吗?还是我只需要添加 [QSA]
  • 它不起作用,它将所有内容重定向到索引:(
猜你喜欢
  • 2014-08-16
  • 2016-05-23
  • 2017-06-25
  • 1970-01-01
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 2016-09-22
  • 1970-01-01
相关资源
最近更新 更多