繁体   English   中英

从特定的网址中使用htaccess删除.php扩展名

[英]remove .php extension using htaccess from a specific url

我在.htaccess文件中写了一些工作正常的规则,我写的规则是:

# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\s/+(\S+?)\.php(/\S*)?\sHTTP [NC]
RewriteRule ^ /%1%2 [L,R=301,NE]

# check to see if the request is for a PHP file:
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^/?(.+?)(/.*)?$ /$1.php$2 [L]

问题:我对他们的唯一问题是,它正在从每个URL中删除.php扩展名,因此我的Ajax请求一直在获取404。

例如:在一个botton上,像Ajax/x.php一样对URL进行Ajax调用,但是由于上述规则,它转换为Ajax/x并返回404。

所以现在我要从所有文件中删除.php扩展名,而不是从所有文件中删除.php扩展名,特别是从2个文件(即a.phpb.php删除。

我需要社区的帮助很少,我知道我已经快到了。任何帮助将不胜感激

要从2个特定文件中删除.php扩展名,可以使用以下命令:

RewriteCond %{THE_REQUEST} /(a|b)\.php [NC]
RewriteRule ^ /%1 [L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [L]
First of all, when you remove the php extension from any url, you have to submit the form by changing removing the .php extension from the action such as
<form class="form-horizontal" method="post" action="new_team" enctype="multipart/form-data">
Same thing also will apply to the ajax file such as
 $.ajax({
            url: 'php/contact_form_submit',
            type: 'post'

.htaccess should look like this
Options +MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule ^([^/]+)/$ http://url/$1 [R=301,L]
RewriteRule ^([^/]+)/$ http://url/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
# RewriteRule ^(.+)\.php$ http://url/$1 [R=301,L]
RewriteRule ^(.+)\.php$ http://url/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

so try it out and let me know if it solved

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM