首页 > 解决方案 > 如何使用 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”。

标签: .htaccess

解决方案


您只需要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]

推荐阅读