【问题标题】:.htaccess not working with php includes.htaccess 不适用于 php 包括
【发布时间】:2017-01-23 12:48:45
【问题描述】:

我正在尝试在我的 .htaccess 中使用 mod_rewrite 但不起作用。

我的网址是http://gestor.samfbas.com.br/index.php?p=something 应该是http://gestor.samfbas.com.br/something

该文件位于我主机的子目录(gestor)中,所有文件都在其中。

<IfModule mod_rewrite.c>
Options +FollowSymLinks +Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ /index.php?p=$1 [L]
</IfModule>

【问题讨论】:

  • 请添加更多信息,我很难找出问题所在。
  • my url is 是什么意思?你把它放在你的html页面上了吗?而这个http://gestor.samfbas.com.br/index.php?p=something 将不会被重定向,因为RewriteCond %{REQUEST_FILENAME} !-f 的条件在这里失败,而这个http://gestor.samfbas.com.br/something 似乎正在工作
  • 阅读:我可以看到你的目录gestor.samfbas.com.br/scripts保护你的服务器删除Options +Indexes
  • @JustOnUnderMillions 我删除了它,谢谢!
  • @JustOnUnderMillions 我的主页是gestor.samfbas.com.br/index.php。当我重定向到某个页面时,我使用条件 ?p=something 但我的 url 不会重写为 /something。

标签: php html .htaccess mod-rewrite


【解决方案1】:

试试这个(如果 index.php 在根文件夹http://gestor.samfbas.com.br/index.php 中):

Options +FollowSymLinks
RewriteEngine On

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

这应该可行。在我的本地机器上测试它(没有其他服务器重定向,否则,只是一个全新的 xampp 安装)。

它将http://gestor.samfbas.com.br/something 重定向到http://gestor.samfbas.com.br/index.php?p=something,而不更改浏览器中的url。

除了评论中的问题。 这个 URL 部分 p= 不应该被外部用户知道! 最好在这里使用像sadff34dngn4nil212ugn= 这样的long var,这样没有人可以直接从外部调用带有参数的index.php。您无法阻止 100%,但重定向参数 p= 仅供内部使用。

但这只是我对此的看法。

希望这会有所帮助。

找到通往罗马的正确道路;)

【讨论】:

  • 如果我有一些东西/page/1,它是如何工作的?我的网址是 gestor.samfbas.com.br/?p=users&page=2 它应该是 gestor.samfbas.com.br/users/page/2
  • @Gabriel Silva 目前,如果你像上面那样使用它,它会重定向到index.php?p=something/page/1 其余的可以完成,通常在 PHP 中完成。
  • 是的,gestor.samfbas.com.br/users/page/2 最终变成了gestor.samfbas.com.br/index.php?p=users/page/2,这是制作动态框架的第一步 :)
  • 可能问题是你目前有这个由 php 生成的gestor.samfbas.com.br/?p=users&amp;page=2,但你想要这个gestor.samfbas.com.br/users/page/2,好的,但这是 Stackoverflow 的一个新问题。提示 str_replace(array('=','&amp;'),'/','?p=users&amp;page=2') 并删除其余部分...
  • 我不明白 =( gestor.samfbas.com.br/users/page/2 应该重定向到 gestor.samfbas.com.br/index.php?p=users&page=2 什么我应该这样做吗?
猜你喜欢
  • 2017-01-22
  • 2014-03-07
  • 1970-01-01
  • 1970-01-01
  • 2018-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多