我正在尝试在我的 .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>
最佳答案
试试这个(如果 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=
,这样就没有人可以直接从外部调用带参数的 index.php。您无法 100% 阻止,但重定向参数 p=
仅供内部使用。
但这只是我对此的看法。
希望对您有所帮助。
找到去罗马的正确方法;)
关于php - .htaccess 不适用于 php 包括,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39512023/