【问题标题】:how to use multiple get requests .htaccess如何使用多个获取请求 .htaccess
【发布时间】:2020-05-13 23:16:55
【问题描述】:

你好,我有个小问题。

当使用.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]

我转换网址 cms/tests/index.php?survey=65 到 -> cms/tests/65

这行得通!

现在我想要一个这样的编辑模式:

cms/tests/65/editcms/tests/65/view

这是我想出的重写规则:

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]

cms/tests/index.php?survey=65&mode=edit 这样编写完整的网址时,它完全可以工作

但它带有“干净”的 url,它会转到我的 404 页面,并在网络选项卡中显示 302 重定向。

有什么我做错了或看多了?

【问题讨论】:

    标签: php html .htaccess


    【解决方案1】:
    #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]
    

    第二个条件永远不会成功,因此不处理规则。

    第二个条件(据说检查请求 + .php 存在)不是你应该在这里做的。如果有的话,您需要检查请求是否未映射到文件。例如:

    RewriteCond %{REQUEST_FILENAME} !-f
    

    但是,如果您通过附加 $(字符串结尾锚)来限制 RewriteRule pattern 中的正则表达式,那么您可以完全删除该条件。例如:

    #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]
    

    与所述正则表达式匹配的请求无法映射到真实文件(除非您有无扩展名的文件)。


    旁白:

    # clean up file extensions .php only
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php [NC,L]
    

    您的第一个代码块中的第二个条件也不是严格正确的,并且在某些条件下会失败,因为文件系统检查不一定与您最终将重写的文件相同,这可能导致 404 或 500 (重写循环)取决于您的文件结构。 (这个代码块出奇的普遍,但是issues relating to this keep on cropping up。)

    这真的应该写成这样:

    # clean up file extensions .php only
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^(.*)$ $1.php [NC,L]
    

    现在,条件%{DOCUMENT_ROOT}/$1.php 匹配重写替换 $1.php。 (假设.htaccess 文件位于文档根目录中。)

    请参阅my answer 到有关 ServerFault 的以下问题,并详细说明此更改:https://serverfault.com/questions/989333/using-apache-rewrite-rules-in-htaccess-to-remove-html-causing-a-500-error

    【讨论】:

    • 非常感谢您的评论
    • 我又遇到了一个小问题...使用您的方法获取请求时,我仍然收到错误:Uncaught SyntaxError: Unexpected token '<' 我认为这是因为出于某种原因它试图将我的 js 文件放在后面我的获取请求image你知道为什么会这样吗?
    • 这很可能是由于在客户端代码中使用相对 URL 路径引起的(导致“404”错误 - 您的代码试图将其解释为 JSON)。您需要在整个过程中使用相对根(以斜杠开头)或绝对 URL。当您位于 URL cms/tests/65/edit 时,客户端资源的任何相对 URL 自然会与此 URL 相关 - 这就是您在屏幕截图中看到的内容。
    猜你喜欢
    • 2019-11-23
    • 1970-01-01
    • 2013-08-11
    • 1970-01-01
    • 2019-02-08
    • 2016-01-11
    • 2021-11-16
    • 2020-08-09
    • 1970-01-01
    相关资源
    最近更新 更多