【问题标题】:Passing multiple parameters in htacess and including files based on them在 htaccess 中传递多个参数并包含基于它们的文件
【发布时间】:2013-09-19 07:27:53
【问题描述】:

我目前正在做一个项目,我必须在 url 中传递三个参数。我的网址看起来像 -

localhost/newproject/index.php?file_name=clients&mode=edit&id=1

下面列出了我在传递不同参数时执行的功能

1) url - localhost/newproject/index.php?file_name=clients

功能 - 包含文件templates/modules/clients/clients.php

2) url - localhost/newproject/index.php?file_name=clients&mode=edit

功能 - 包含文件 template/modules/clients/cliemts-edit.php。由于未设置客户端 ID,因此客户端编辑屏幕将用于添加新客户端。

3) url - localhost/newproject/index.php?file_name=clients&mode=edit&id=1

功能 - 包含文件 template/modules/clients/cliemts-edit.php。并且客户端编辑屏幕将用于更新其 client_id 为 1 的客户端。现在,我正在尝试将此 url 转换为此

localhost/newproject/clients/edit/1

我的 htaccess 中有以下代码

RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ index.php?file_name=$1&mode=$2&id=$3 [L]
RewriteRule ^([^/\.]+)$ index.php?file_name=$1 [L]

我面临的问题是,每当包含客户端编辑屏幕时,css 文件、javascript 文件和图像的路径都会更改。我该如何克服呢?

【问题讨论】:

标签: php .htaccess mod-rewrite


【解决方案1】:

你在正确的轨道上。

试试:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ index.php?file_name=$1&mode=$2&id=$3 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)$ index.php?file_name=$1&mode=$2 [L]

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

为了修复 css/脚本,您需要将链接设为绝对 URL(它们以 / 开头)或在页面标题中包含相对 URI 基础:

<base href="/" />

【讨论】:

  • 谢谢乔恩。我实际上已经在配置文件中使用 $_SERVER["HTTP_HOST"] 定义了路径并将它们用作路径。页面仍然无法找到 css 文件。我收到一个错误,表明该页面实际上是在页面目录中查找 css 文件。一直无法弄清楚问题可能是什么。
  • @AnshulKumar 如果您右键单击图像并复制 URL,那会是什么样子?它应该是什么样子?
  • 乔恩:已经修好了。非常感谢你的帮助。早些时候,图片的链接看起来像“locahost/newproject/templates/clients/images/icon1.png”,而应该是“locahost/newproject/images/icon1.png
猜你喜欢
  • 2020-10-09
  • 2020-12-27
  • 2015-12-21
  • 2022-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多