如何删除php扩展名以及如何将扩展名重定向到无扩展名形式 [英] How to remove php extension and also redirect extensions to no extension form
问题描述
我该如何在htaccess中使用它:
How can I have this in my htaccess:
/page.php => redirects to /page
/page => shows the page.php
/index => redirects to root
以及如何从所有URL(特别是根URL)中删除尾部斜杠?
And how to remove trailing slashes from all URLs, specially the root one?
尽管,我对Apache配置还不太熟悉,但是我可以找到以下代码:
Although, I'm not familiar enough with Apache configs, but I could find these codes:
# To remove .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# To remove trailing slashes
RewriteRule ^(.*)/$ /$1 [L,R=301]
# To redirect /index to root (not sure if it works correctly)
RewriteCond %{THE_REQUEST} ^.*/index
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
能否请您帮我解决这个问题,并用一个小的代码完成它?
Could you please help me with the scenario and have it in one small code?
谢谢
根据答案,我可以使用.htaccess文件中的以下代码解决问题:
According to answers, I could solve the problem using the codes below in my .htaccess file:
# To remove .php extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ (.*)\.php [NC]
RewriteRule ^ %1 [R=301,L]
# To remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]
# To check whether the file exists then set it back internally
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^.*$ $0.php [L]
# To redirect /index to root
RewriteCond %{THE_REQUEST} ^.*/index
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
现在,我遇到了一个新问题,那就是当我用斜杠浏览文件时,它会将我重定向到其他地方,并出现404(找不到页面)的响应,如下所示:
Now, I got a new problem and that is when I browse the files with a trailing slash, it redirects me to somewhere else and occurs a 404 (page not found) response, like this:
http://domain.tld/page/ => http://domain.tld/page/home/user/public_html/domain.tld/page
此外,如果我将第二部分移到末尾甚至完全删除,服务器也会向我发送500(服务器内部错误)响应.
Also, if I move the second part to the end or even remove it completely, the server sends me a 500 (internal server error) response.
最后通过一些研究,我可以使用以下建议找到先前问题的解决方案:
Finally with a little research, I could find the solution for previous problem using this advice:
https://stackoverflow.com/a/27264788/5420319
因此,我从中更改了其中一个代码:
So, I've changed one of the codes from this:
# To remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]
对此:
# To remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*)/$
RewriteRule ^ /%1 [R=301,L]
不管效果如何,我也将这部分移到顶部.
And I also moved this part to the top regardless of being effective.
推荐答案
根据答案和我自己的研究,这将是最终答案:
According the the answers and my own research, this would be the final answer:
# To remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*)/$
RewriteRule ^ /%1 [R=301,L]
# To remove .php extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ (.*)\.php [NC]
RewriteRule ^ %1 [R=301,L]
# To check whether the file exists then set it back internally
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^.*$ $0.php [L]
# To redirect /index to root
RewriteCond %{THE_REQUEST} ^.*/index
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
这篇关于如何删除php扩展名以及如何将扩展名重定向到无扩展名形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!