mod_rewrite 和链接的问题

发布于 2024-10-01 21:56:18 字数 733 浏览 0 评论 0原文

我正在开发一个基于 MVC 的应用程序,现在我已经设置了 .htaccess 重写规则。但是,我无法使所有内容正确显示。 这是我的 .htaccess:

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

我遇到了链接问题,因为起初 URL 类似于“domain.com/folder/index.php?a=whatever/whatever2/asd”,但现在它们是“domain.com/”文件夹/whatever/whatever2/asd”。

我生成的链接就像“Qwerty”,但现在当我尝试生成链接时,我必须执行类似“Qwerty”之类的操作,你猜怎么着?例如,如果我在“domain.com/folder/new/lol”上,然后单击此类链接,我将被重定向到“domain.com/folder/new/new/qwerty”,并且它不是并不意味着要那样工作。

我不能使用绝对路径,因为该软件对于下载它的用户来说是易于安装的,而且我不知道用户将在哪里安装该软件,也不希望用户必须修改该软件的任何内容代码。

提前致谢,

伊万

I'm developing a MVC-based application and I've now set the .htaccess rewrite rules. However, I'm having problems to make everything appear correctly.
This is my .htaccess:

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

I'm having problems with the links as at first the URLs were like "domain.com/folder/index.php?a=whatever/whatever2/asd", but now they are "domain.com/folder/whatever/whatever2/asd".

I was generating the links just like "<a href='index.php?a=new/qwerty'>Qwerty</a>", but now when I try to generate the links I must do something like "<a href='new/qwerty'>Qwerty</a>", and guess what? If I'm, for example, on "domain.com/folder/new/lol" and I click on that kind of link, I will get redirected to "domain.com/folder/new/new/qwerty" and it isn't meant to work like that.

I can't use absolute paths as the software is meant to be easy-to-install for users who download it, and I can't know where the users will install this software, nor I want users to have to modify anything of the code.

Thanks in advanced,

Ivan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北斗星光 2024-10-08 21:56:18

我终于解决了。

我制作了自己的链接生成器函数,并使其从 $_SERVER['SCRIPT_NAME'] 中删除 index.php 部分(这为我们提供了脚本的绝对路径)。
如果您有多个脚本,只需从该特殊变量中删除每个可能的脚本即可。

function url($rel_path) {
  $start = str_replace("index.php", "", $_SERVER['SCRIPT_NAME']);
  return $start.$rel_path;
}

I finally solved it.

I made my own link generator function and I made it to delete the index.php part from $_SERVER['SCRIPT_NAME'] (which gives us the absolute path to the script).
In case you had more than one script, just delete every possible script from that special variable.

function url($rel_path) {
  $start = str_replace("index.php", "", $_SERVER['SCRIPT_NAME']);
  return $start.$rel_path;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文