修改 htAccess 以从 URL 中提取和删除整数值


modify htaccess to extract and remove integer-value from url

我有一个像下面的htaccess文件:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?$1=$1&pid=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ /index.php?pid=$1 [L,QSA]
它将像fe"

administration/modules"这样的链接定向到"index.php/administration=administration&pid=modules"或"administration"到"index.php?pid=admin"。

出于性能原因,我想修改 htaccess,以便它可以定向一个链接,例如

"administration_55"

索引.php?pid=55"

因此,必须以某种方式从 pid 参数"pid=administration_55"中提取"55"(记录的 ID)。由于我还希望有一个像"www.example.com/administration"这样的可见 url 而没有"_55"部分,我想知道,这将如何以一种好的方式完成,直接在 htaccess 文件中或在带有标题重定向的响应 php 文件中?

提前感谢,杰登

# e.g. /administration/modules 
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?$1=$1&pid=$2 [L,QSA]
# e.g. /administration_55
RewriteRule ^([^/]+)_([[0-9]+)/?$ /index.php?$1=$1&pid=$2 [L,QSA]
# e.g. /test
RewriteRule ^([^/]+)/?$ /index.php?pid=$1 [L,QSA]

至于删除 55,最好的方法是使用 PHP。我的数据库表中有一个名为"url"的单独字段,因此我可以找到具有正确url(-part)的记录。为了提高性能,我还在此字段中添加了索引。

仅仅因为你可以并不意味着你应该...您不能根据站点本身的索引.php在索引中处理此问题REFERRER,这将更安全并且不需要启用.htaccess支持...?

您可以使用 htaccess 重定向,但您必须包含带有 _55 部分的 url