如何从URL中删除index.php并隐藏参数lang?

时间:2019-09-13 01:08:30

标签: php .htaccess mod-rewrite url-rewriting

我有以下网址

str.format

我想使用.htaccess使其类似于以下内容

http://localhost/apps/site/index.php?lang=es

.htaccess规则

http://localhost/apps/site/es

当我尝试使用此网址<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-l RewriteRule ^(.+?)/?$ /index.php?lang=$1 [L,QSA] </IfModule> 时 直接将我重定向到http://localhost/apps/site/es

我已经尝试过此解决方案,但不适用于我:

https://stackoverflow.com/a/30153794/7705186

1 个答案:

答案 0 :(得分:0)

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/apps/site/index.php$
RewriteCond %{QUERY_STRING} ^lang=(\w+)
RewriteRule apps/site/index.php /apps/site/%1 [QSD,L]
  • 第一个条件是告诉模块仅在请求URI恰好/apps/site/index.php

  • 时应用规则
  • 第二个条件,如果第一个查询参数名称为lang并且其值是一个单词。 ()将捕获参数的值并将其保存在%1

  • 在规则中,我们告诉模块删除apps/site/index.php并用/apps/site/%1重写,同时用我们从第二个条件捕获的值替换%1

    < / li>
  • QSD查询字符串丢弃标志是为了使模块不将查询附加到最终URL(必须在L标志之前进行设置,这将 >工作[L,QSD]

现在,如果您要求

http://localhost/apps/site/index.php?lang=es

它将被重写为

http://localhost/apps/site/es

您可以在此网站https://htaccess.madewithlove.be/上测试并尝试使用重写规则