.htaccess在斜杠后删除所有.php和id作为默认参数 [英] .htaccess remove all .php and id as default parameter after slash

查看:82
本文介绍了.htaccess在斜杠后删除所有.php和id作为默认参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 .htaccess 文件,可帮助我重写 http://domain/path/project.php?id = 1 作为 http:// domain / path / project / 1

I have the following .htaccess file which helps me to rewrite the http://domain/path/project.php?id=1 as http://domain/path/project/1.

RewriteBase /
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ project.php?id=$1 [QSA,NC,L]

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ project.php [QSA,NC,L]

RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ project.php [QSA,NC,L]   

但是我需要以相同的方式替换当前目录中的所有php。我有 edit_project.php?id = 1 delete_project.php?id = 1 等。 id 是除某些PHP以外所有PHP的默认参数。因此,我希望htaccess执行以下操作。

But I need all the php's in the current directory to be replaced in the same way. I have edit_project.php?id=1, delete_project.php?id=1 etc. id is a default parameter for all the php's except some. So I want the htaccess to do the following.

http://domain/path/*.php?id = 1 http:// domain / path / * / id
,当没有id时,它应该看起来像 http:// domain / path / * ,并且当有其他参数时,它应类似于 http:// domain / path / *?param = value

http://domain/path/*.php?id=1 as http://domain/path/*/id and when there is no id, it should just look like http://domain/path/* and when there are other parameters, it should look like http://domain/path/*?param=value

由于我是.htaccess的新手,所以我们将不胜感激。还请解释一下这是如何工作的。

Since I am new to .htaccess, any help would be appreciated. Also please explain me how this works.

推荐答案

您可以使用以下内容:

RewriteEngine On
RewriteBase /    
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^path/([^/]+)/?([0-9]*)/?$ $1.php?id=$2 [QSA,NC,L]

这会将 / path / file / id 重写为 /file.php?id=id

因为$ 1表示第一组([^ /] +)的匹配,而$ 2表示第二组的匹配([0 -9] *)

Since $1 represents match of first group of ([^/]+) and $2 represents match of second group of ([0-9]*)

这篇关于.htaccess在斜杠后删除所有.php和id作为默认参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆