Mod rewrite 特定的URL重写情况

Mod rewrite 特定的URL重写情况,mod-rewrite,url-rewriting,apache2,Mod Rewrite,Url Rewriting,Apache2,我正试图在我的Apache2服务器上实现这一点: 任何时候输入这样的url: http://www.mydomain.com/first_location mod_重写应重定向到: http://www.mydomain.com/first_location/first_location.php 其中“first_location”可以是任何有效的文件夹名称 我已经收集到了这么多: RewriteRule ^[A-Za-z0-9-]/ ....... 但不确定在这种情况下如何处理重定向ur

我正试图在我的Apache2服务器上实现这一点:

任何时候输入这样的url:

http://www.mydomain.com/first_location
mod_重写应重定向到:

http://www.mydomain.com/first_location/first_location.php
其中“first_location”可以是任何有效的文件夹名称

我已经收集到了这么多:

RewriteRule ^[A-Za-z0-9-]/ .......
但不确定在这种情况下如何处理重定向url部分


感谢您为完成替换部分所提供的任何帮助(如有必要,也请更正模式部分)。

如果
first\u location
是一个有效的文件夹,则有一个名为mod\u dir的apache模块,该模块将自动将浏览器重定向到同一文件夹,但带有尾随斜杠。因此,您可以使用
DirectorySlash off
关闭mod_dir,或者允许重定向并仅与尾部斜杠匹配。因此,您可能需要类似于此的内容:

RewriteEngine On
# check that it's a valid folder
RewriteCond %{REQUEST_FILENAME} -d
# check that the php file actually exists:
RewriteCond %{REQUEST_URI} ^/([^/]+)/$
RewriteCond %{REQUEST_FILENAME}%1.php -f
# rewrite
RewriteRule ^/?([^/]+)/$ /$1/$1.php [L]

谢谢我在一个新的.htaccess文件中处理并粘贴了它,但它给了我500个错误。我已经检查了apache配置文件,并且mod_dir load命令未注释(因此处于活动状态)。@user1729972您需要检查mod_rewrite是否也未注释哦,这是最肯定的。mod_rewrite适用于其他情况,特别是不适用于此。@user1729972似乎我缺少一个接近的参数,请再试一次