简体   繁体   English

如何使用.htaccess从网址中删除扩展名而没有结尾斜杠?

[英]How to remove extension from url WITHOUT trailing slash using .htaccess?

I have a .htaccess file: 我有一个.htaccess文件:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

And I have a file called random.php . 我有一个名为random.php的文件。

All I want to do is just call something.com/random , but Apache adds a trailing slash (using 301 Redirect) to the end of the URL, therefore giving an error stating that something.com/random/.php cannot be found. 我要做的只是调用something.com/random ,但是Apache在URL的末尾添加了斜杠(使用301重定向),因此出现错误,指出无法找到something.com/random/.php

EDIT 1: When I use 编辑1:当我使用

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [NC,L]

then my external files (.js, .css, etc.) can't load and the server responds with a 500 Internal Server Error response. 那么我的外部文件(.js,.css等)将无法加载,并且服务器会以500 Internal Server Error响应进行响应。 Apache says that there were too many redirects. Apache说重定向太多。

Try this rule with optional trailing slash: 尝试使用此规则和可选的斜杠:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f    
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

You almost had it right, you just needed to exclude the slash as well: 您几乎是对的,您也只需要排除斜线即可:

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.\/]+)$ $1.php [NC,L]    

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

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