【发布时间】:2023-03-20 05:25:01
【问题描述】:
这是我想要实现的目标:
浏览器请求:example.com/webApp/controller/action/?param1=param1value
- URL 的静态(始终相同)部分是
webApp/ - URL 的动态部分是
controller/action/?param1=param1value。通常它由REQUEST_URI(由不同数量的部分组成的路径)和QUERY_STRING组成,它可以是任何东西,例如:site/index/?page=2books/index/?page=2&sort=nameusers/listfoo/baruser/profile/posts/?page=2
- 上面列出的 URL 在 .htaccess(服务器接收的内容)之后应如下所示:
index.php?path=site/index/page/2index.php?path=books/index/page/2/sort/nameindex.php?path=users/listindex.php?path=foo/barindex.php?path=user/profile/posts/page/2
- 当用户在浏览器中看到没有
index.php部分的 URL 时:site/index/page/2books/index/page/2/sort/nameusers/listfoo/baruser/profile/posts/page/2
基本上,我想通过将所有? 和& 字符替换为/ 并将其动态附加到REQUEST_URI 来清理URL 的QUERY_STRING 部分。
我一直在做很多关于这个主题的搜索。不幸的是,最接近的匹配是硬编码 QUERY_STRING 参数转换为路径的示例。相反,我想将所有 QUERY_STRING 部分(如果有的话)动态地转换为路径。
我目前的.htaccess文件内容如下:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.jpeg|\.bmp|\.css|\.js)$
RewriteRule ^([^?]*)$ index.php?path=$1 [NC,L,QSA]
唯一缺少的部分是清理QUERY_STRING,如上所述。此外,如果请求 URI 是上面代码中列出的文件(扩展名列表),则不会通过 index.php 处理。
【问题讨论】:
标签: .htaccess redirect mod-rewrite clean-urls