php - 使用 mod_rewrite 将文件夹转换为查询字符串

标签 php .htaccess mod-rewrite

转换这些请求的最简单方法是什么:

http://www.example.com/en/
http://www.example.com/en/index.php

致:

http://www.example.com/index.php?language_id=en

PHP 文件多种多样,因此规则也需要映射

http://www.example.com/en/anyfile.php

致:

http://www.example.com/anyfile.php?language_id=en

语言 ID 可以是任意两个字母代码(在 PHP 脚本中进行检查,以确保其有效)。

据推测,我需要使用 .htaccess,并且任何有关如何实现上述内容的建议将不胜感激。谢谢。

更新

以下内容对我有用。

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ([a-z]+)/(css|js|images)/(.+) /$2/$3 [L]
  RewriteRule ([a-z]+)/(.+).php /$2.php?language_id=$1 [QSA,L]
  RewriteRule ([a-z]+)/$ /index.php?language_id=$1 [QSA,L]
</IfModule>

这还会将对 css、js 和 images 文件夹的请求重新路由回根文件夹。如果没有输入文件名,它也可以工作,自动重定向到index.php。

最佳答案

您可能希望将 QSA 标志添加到重写规则中,这会将查询字符串上的任何附加位附加到重写的请求中,例如

RewriteRule magic here [PT,QSA]

关于php - 使用 mod_rewrite 将文件夹转换为查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/710062/

相关文章:

php - 你如何验证一个属性是否可以在 php 中访问?

php hex to dec with math functions 返回不同的结果

laravel - HTAccess 将除特定 URL 之外的所有内容重定向到文件夹

.htaccess - mod_rewrite : Redirect request to subdir with many subfolders of different structures + redirect to index. php

apache - HTTP ://www redirect to https://

PHP 字符串 - 删除数组或单词并检查其他

php - 使用 jQuery 对话框显示 mysql_error 消息

.htaccess - htaccess 子域重写

.htaccess RewriteRule 重写我的 css、js 和图像文件

php - htaccess 使用 ssl 认证从 www 重定向到非 www url