mod_rewrite 和链接的问题
我正在开发一个基于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于解决了。
我制作了自己的链接生成器函数,并使其从 $_SERVER['SCRIPT_NAME'] 中删除 index.php 部分(这为我们提供了脚本的绝对路径)。
如果您有多个脚本,只需从该特殊变量中删除每个可能的脚本即可。
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.