.htaccess - 如何使用 htaccess 在 url 中添加斜杠?
问题描述
我正在使用以下 htaccess 代码来重定向页面而不使用扩展名。它工作正常,但现在我需要在 url 末尾添加斜杠如何添加我尝试了很多没有任何工作。
这是我的代码
#Alternate default index page
DirectoryIndex index.php
RewriteEngine On
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
在使用此配置时,我还有另一个问题。我试图通过 ajax 发布请求调用“api/apis.php”文件,但请求失败。请求转换为 GET,如“api/apis”。
解决方案
您只需要POST
在第一个重定向规则中禁用方法:
DirectoryIndex index.php
RewriteEngine On
# browser requests PHP if request method is not POST
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.+)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
推荐阅读
- function - 事件场景内的电晕功能位置
- r - 在 R 中使用聚合时如何保留其他列?
- django - 使用 Gunicorn 和 Nginx 在一个 Droplet 上运行两个 Django 项目
- r - 使用 mutate_at 缩短多个 dplyr 变异?
- php - 喜欢 jQuery 求和值
- javascript - 为什么我不能将图表添加到动态插入的 svg 元素?
- r - R,ImageMagick:如何在动画中的每个循环后添加延迟
- c# - 如何根据访问级别显示菜单选项?
- powerbi - POWER BI - 创建一个度量来比较预测与销售额
- javascript - 默认接收器上的 Chromecast WebVTT 字幕