Php 如何使用多个get请求。htaccess
你好,我有点小问题 当使用Php 如何使用多个get请求。htaccess,php,html,.htaccess,Php,Html,.htaccess,你好,我有点小问题 当使用.htaccess RewriteEngine on Options +FollowSymLinks # clean up file extensions .php only RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(.*)$ $1.php [NC,L] #make get request clean RewriteCond %
.htaccess
RewriteEngine on
Options +FollowSymLinks
# clean up file extensions .php only
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
#make get request clean
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^cms/tests/([0-9]+) cms/tests/index.php?survey=$1 [NC,L]
我转换url
cms/tests/index.php?survey=65
to->cms/tests/65
这管用
现在我想要一个如下的编辑模式:
# clean up file extensions .php only
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
cms/tests/65/编辑
或cms/tests/65/查看
这是我提出的重写规则:
RewriteEngine on
Options +FollowSymLinks
# clean up file extensions .php only
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
#make get request clean
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^cms/tests/([0-9]+)/([a-zA-Z0-9_-]+) cms/tests/index.php?survey=$1&mode=$2 [NC,L]
当像这样编写完整的urlcms/tests/index.php?survey=65&mode=edit
时,它完全可以工作
但它与“干净”的url一起进入我的404页面,并在网络选项卡中显示302重定向
是否有我做错了或看过头的事情
第二个条件永远不会成功,因此不处理该规则
第二个条件(假定检查请求+.php
是否存在)不是您应该在这里执行的操作。如果有的话,您需要检查请求是否没有映射到文件。例如:
RewriteCond %{REQUEST_FILENAME} !-f
#make get request clean
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^cms/tests/([0-9]+)/([a-zA-Z0-9_-]+)$ cms/tests/index.php?survey=$1&mode=$2 [NC,L]
但是,如果通过附加$
(字符串结束锚定)来限制重写规则
模式中的正则表达式,则可以完全删除该条件。例如:
RewriteCond %{REQUEST_FILENAME} !-f
#make get request clean
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^cms/tests/([0-9]+)/([a-zA-Z0-9_-]+)$ cms/tests/index.php?survey=$1&mode=$2 [NC,L]
与所述正则表达式匹配的请求无法映射到实际文件(除非您有无扩展文件)
旁白: 第一个代码块中的第二个条件也不是严格正确的,在某些条件下会失败,因为文件系统检查不一定与最终要重写到的文件相同,这可能导致404或500(重写循环),具体取决于文件结构。(这个代码块非常常见,但是) 这应该写得更像这样:
# clean up file extensions .php only
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
现在,条件%{DOCUMENT\u ROOT}/$1.php
与重写替换$1.php
匹配。(假设.htaccess
文件位于文档根目录中。)
请参阅以下有关ServerFault的问题,并详细解释此更改:非常感谢您的评论,我还有一个小问题。。。在使用get请求的方法时,我仍然会收到一个错误:
Uncaught SyntaxError:Unexpected token',这很可能是由于在客户端代码中使用相对URL路径造成的(导致“404”错误-您的代码试图将其解释为JSON)。您需要在整个过程中使用根相对URL(以斜杠开始)或绝对URL。当您处于URLcms/tests/65/edit
时,客户端资源的任何相对URL自然都会与此URL相对,这就是您在屏幕截图中看到的。