要使用htaccess RewriteCond重写的PHP查询字符串


PHP query strings to be rewritten with htaccess RewriteCond

我在index.php上有一个switch,它监听?action=

我想rewrite index.php/index.php?action= 之后的任何内容

目前我有一个规则来删除index.php

RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{DOCUMENT_ROOT}/$1'.php -f [NC] RewriteRule ^(.+?)/?$ /$1.php [L]

所以当前的url看起来像;

localhost:8888/?action=viewBasket

并希望将其改写为;

localhost:8888/viewBasket

我很困惑是否需要为每个开关参数创建一个条件,以防止图像、脚本、源代码等被重写。或者是否有条件先检查exisit是否存在。干杯

我已经解决了。如果没有人能提供更好的独奏,我明天会把我的问题去掉。

# redirect to any directories
RewriteRule ^styles/(.*) styles/$1 [L,QSA]
RewriteRule ^styles/fonts/(.*) styles/fonts/$1 [L,QSA]
RewriteRule ^images/(.*) images/$1 [L,QSA]
RewriteRule ^bower_components/(.*) bower_components/$1 [L,QSA]
RewriteRule ^scripts/(.*) scripts/$1 [L,QSA]
#if not a directory listed above, rewrite the request to ?action=
RewriteRule ^(.*)$ index.php?action=$1 [L,QSA]

试试这个:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?action=$1 [L,QSA]

试试这个:

RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php?action=$1 [L]

第一行:添加这个是因为我在挠头为什么它对我不起作用。我相信你知道你需要这个,只是给其他可能看到这个的人的一个提示。

第二行:检查它是否不是文件。

第三行:检查它是否不是目录。如果是文件或目录,它将不会处理您的RewriteRule s。默认情况下,RewriteCond s使用和操作。

第四行:我相信有人能写出更好的规则。[L] 最后一条规则,停止计算"重写规则"。告诉重写到index.php?action={whatst},而不更改位置标头。

第四行的要点是:如果你的index.php是

var_dump($_GET);

www.example.com/foo

将产生array(1) { ["action"]=> string(3) "foo" }