如何在.htaccess文件中使用RewriteEngine

时间:2017-10-12 20:09:35

标签: php apache .htaccess mod-rewrite

我当前的.htaccess文件包含此代码

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} .*jpg$|.*gif$|.*png$|.*mp3$ [NC]
RewriteRule (.*) /viewed.php?file=$1

我想更改它,以便将URL重写为/viewed.php?file=$1($ 1为文件路径),如果扩展名为 not .php,但我不知道知道该怎么做。 (我在另一篇文章中找到了代码,并将自己的扩展名添加到列表中)

如果有人可以将我与“重写条件”的工作原理联系起来,那将是很好的,因为我不明白如何制作它们。

1 个答案:

答案 0 :(得分:1)

重写全部,如果不是.php

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^.*\.php$ [NC]
RewriteRule (.*) /viewed.php?file=$1 [L]

文档: http://httpd.apache.org/docs/current/mod/mod_rewrite.html

了解RegEx: https://regexone.com/