PHP 要使用 htaccess RewriteCond 重写的查询字符串
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}/\.php -f [NC]
RewriteRule ^(.+?)/?$ /.php [L]
所以当前的 url 看起来像;
localhost:8888/?action=viewBasket
并想将其重写为;
localhost:8888/viewBasket
我对是否需要为每个开关参数创建条件感到困惑,以防止图像、脚本、源等被重写。或者如果有条件先检查是否存在。干杯!
我已经解决了。如果没有人提供更好的解决方案,明天将删除我的问题。
# redirect to any directories
RewriteRule ^styles/(.*) styles/ [L,QSA]
RewriteRule ^styles/fonts/(.*) styles/fonts/ [L,QSA]
RewriteRule ^images/(.*) images/ [L,QSA]
RewriteRule ^bower_components/(.*) bower_components/ [L,QSA]
RewriteRule ^scripts/(.*) scripts/ [L,QSA]
#if not a directory listed above, rewrite the request to ?action=
RewriteRule ^(.*)$ index.php?action= [L,QSA]
试试这个:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?action= [L,QSA]
试试这个:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?action= [L]
第一行:添加这个是因为我在摸不着头脑,为什么它对我不起作用。我相信你知道你需要这个,只是给可能查看这个的其他人的注释。
第二行:检查它是否不是一个文件。
第三行:检查是否不是目录。如果它是文件或目录,它不会处理您的 RewriteRule
s。默认情况下 RewriteCond
s 用 and 操作。
第四行:我相信有人可以写出更好的规则。 [L] 最后一条规则,停止评估 RewriteRules。告诉它重写 index.php?action={whatever} 而不改变位置 header.
第四行的要点是:如果你的index.php是
var_dump($_GET);
www.example.com/foo
将产生 array(1) { ["action"]=> string(3) "foo" }
我在 index.php
上有一个 switch
,它监听 ?action=
我想 rewrite
index.php/
到 index.php?action=
目前我有一个规则要删除 index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/\.php -f [NC]
RewriteRule ^(.+?)/?$ /.php [L]
所以当前的 url 看起来像;
localhost:8888/?action=viewBasket
并想将其重写为;
localhost:8888/viewBasket
我对是否需要为每个开关参数创建条件感到困惑,以防止图像、脚本、源等被重写。或者如果有条件先检查是否存在。干杯!
我已经解决了。如果没有人提供更好的解决方案,明天将删除我的问题。
# redirect to any directories
RewriteRule ^styles/(.*) styles/ [L,QSA]
RewriteRule ^styles/fonts/(.*) styles/fonts/ [L,QSA]
RewriteRule ^images/(.*) images/ [L,QSA]
RewriteRule ^bower_components/(.*) bower_components/ [L,QSA]
RewriteRule ^scripts/(.*) scripts/ [L,QSA]
#if not a directory listed above, rewrite the request to ?action=
RewriteRule ^(.*)$ index.php?action= [L,QSA]
试试这个:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?action= [L,QSA]
试试这个:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?action= [L]
第一行:添加这个是因为我在摸不着头脑,为什么它对我不起作用。我相信你知道你需要这个,只是给可能查看这个的其他人的注释。
第二行:检查它是否不是一个文件。
第三行:检查是否不是目录。如果它是文件或目录,它不会处理您的 RewriteRule
s。默认情况下 RewriteCond
s 用 and 操作。
第四行:我相信有人可以写出更好的规则。 [L] 最后一条规则,停止评估 RewriteRules。告诉它重写 index.php?action={whatever} 而不改变位置 header.
第四行的要点是:如果你的index.php是
var_dump($_GET);
www.example.com/foo
将产生 array(1) { ["action"]=> string(3) "foo" }