如何隐藏 URL 上的.php和 ID


how to hide .php and id on url

我想更改此网址:

http://example.com/pages.php?sports_id=23

进入这个:

http://example.com/pages/sports/23

我的代码是:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1'.php

ErrorDocument 404 /404.php

将此行添加到您的 .htacess

RewriteRule ^pages$ pages.php [L]

但我认为不可能将 GET 部分更改为斜杠。(如果我错了,请纠正我)。

如果还没有,首先需要将应用程序中的 URL 更改为 /pages/sports/23 格式。然后,您可以使用 .htaccess 将"漂亮"的 URL 重写回真实 URL。

要从字面上将/pages/sports/23重写回/pages.php?sports_id=23然后您可以执行以下操作:

RewriteRule ^pages/sports/23$ pages.php?sports_id=23 [L]

但是,您可能想要更通用的东西。但是有多通用?从您的问题中不清楚哪些元素是可变的。类似以下内容的内容可能适合您...

RewriteRule ^([^/])+/([^/])+/('d{3})+$ $1.php?$2_id=$3 [L]