Php 如何在.htaccess中编写mod_重写规则

Php 如何在.htaccess中编写mod_重写规则,php,apache,.htaccess,redirect,mod-rewrite,Php,Apache,.htaccess,Redirect,Mod Rewrite,我正在使用htaccess文件中的mod_rewrite将一些URL重定向到特定页面。该项目是在核心PHP(无框架)中开发的,我正在xampp服务器上工作 我想把下面的url重定向到test.php localhost/project/any_string //should be redirected to test.php localhost/project/any_string/test //should be redirected to new_test.php 及 我已启用mod_重写

我正在使用htaccess文件中的mod_rewrite将一些URL重定向到特定页面。该项目是在核心PHP(无框架)中开发的,我正在xampp服务器上工作

我想把下面的url重定向到test.php

localhost/project/any_string //should be redirected to test.php
localhost/project/any_string/test //should be redirected to new_test.php


我已启用mod_重写,如有任何帮助,将不胜感激

您是否正在寻找以下内容

RewriteEngine On
RewriteRule localhost/project/(.*)/test new_test.php [R]
RewriteRule localhost/project/(.*) test.php [R]
RewriteRule localhost/project/(.*)/(.*).php $1/$2.php [R]
RewriteRule localhost/project/(.*).php $1.php [R]
[R]标志强制重定向。
请注意,较长的匹配项在较短的匹配项之前列出(否则,较长的匹配项将永远不会匹配)。

如果我了解您需要什么,这将实现:

RewriteEngine On
RewriteRule ^(.*)project/(.*)/(.*).php$ /$2/$3.php [L,R=301]
RewriteRule ^(.*)project/(.*).php$ /$2.php [L,R=301]
RewriteRule ^(.*)project/(.*)test$ /new_test.php [L,R=301]
RewriteRule ^(.*)project/(.*)$ /test.php [L,R=301]

顺便说一句,这里的规则顺序非常重要,因此您必须保持完全相同。

我尝试了您的解决方案,但对我不起作用。您将.htaccess文件放在了哪个目录中,并且在父vhost中设置了AllowOverride吗?我不认为@avinashhiremath正在寻找重定向。我确信,这里的目的是对PHP文件进行内部重写。另外,您在重写模式中包含了
localhost
,这是无效的。我已将.htaccess放置在项目目录中,并已将Allowoverride All设置在httpd xampp中。conf@Mike,我希望将
localhost/project/any_string
重定向到
localhost/project/test.php
'RewriteRule^([a-z0-9\-])test.php[L,NC]'
RewriteEngine On
RewriteRule ^(.*)project/(.*)/(.*).php$ /$2/$3.php [L,R=301]
RewriteRule ^(.*)project/(.*).php$ /$2.php [L,R=301]
RewriteRule ^(.*)project/(.*)test$ /new_test.php [L,R=301]
RewriteRule ^(.*)project/(.*)$ /test.php [L,R=301]