你好,我有个小问题。
当使用这些行的.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转换为-> cms/tests/65。
这行得通!
现在,我希望有这样的编辑模式:
cms/tests/65/edit或cms/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时,它完全可以工作。
但是它带有“干净”的url,它会转到我的404页面,并在网络选项卡中显示302重定向。

有什么事情是我做错了还是看错了?
发布于 2020-01-28 17:10:02
#make RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^cms/test/(0-9+)/(a-Za-Z0-9-+)cms/test/index.php?
第二个条件永远不会成功,因此规则不会被处理。
第二个条件(应该检查请求+ .php是否存在)不是您在这里应该做的事情。如果有的话,您需要检查请求是否映射到文件。例如:
RewriteCond %{REQUEST_FILENAME} !-f但是,如果您在RewriteRule模式中通过附加一个$ (字符串结束锚)来限制正则表达式,那么您可以完全删除该条件。例如:
#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]如前所述,匹配regex的请求不能映射到真正的文件(除非您有无扩展名的文件)。
撇开:
只清理文件扩展名.php %{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
https://stackoverflow.com/questions/59950472
复制相似问题