htaccess 重写规则:显示页面而不在 url

时间:2021-04-21 14:54:04

标签: apache .htaccess mod-rewrite

我有一个 php 脚本,它可以根据 QUERY_STRING

显示一些图像

示例:

<块引用>

example.com/img.php?IMAGE_1

但我希望即使用户访问也能显示相同的结果

<块引用>

example.com/IMAGE_1

我在论坛上尝试过此代码,但它不起作用

RewriteEngine On  # enables url rewriting

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php  

1 个答案:

答案 0 :(得分:1)

就这样吧:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ img.php?$1 [L,QSA]

此规则匹配 URI 中不能匹配现有文件或目录的任何单词字符或连字符的 1+。如果匹配成功,则使用 img.php 将路径转换为查询字符串。