我们如何编写这样的PHP脚本?
有时我们会看到包含以下URL的网站: somesite.com/123.php somesite.com/124.php somesite.com/125.php “.php”前面的数字是Mysql查询的ID somesite.com/123.php=somesite.com/file.php?id=123 我注意到这些文件[123.php,124.php,125.php]并没有真正存储在服务器中我们如何编写这样的PHP脚本?,php,.htaccess,Php,.htaccess,有时我们会看到包含以下URL的网站: somesite.com/123.php somesite.com/124.php somesite.com/125.php “.php”前面的数字是Mysql查询的ID somesite.com/123.php=somesite.com/file.php?id=123 我注意到这些文件[123.php,124.php,125.php]并没有真正存储在服务器中 我们该怎么做呢?您必须使用一些http重定向和一个.htaccess文件(在linux上)…用于a
我们该怎么做呢?您必须使用一些http重定向和一个.htaccess文件(在linux上)…用于apache,这些重定向通常放在
.htaccess
文件中。在您的特定示例中,您应该在.htaccess文件中使用重写规则,如下所示:
RewriteRule ^/somesite\.com/([0-9]+)\.php/$ file.php?id=$1
.htaccess文件使用mod_rewrite的重写规则 有几个链接可以帮助您开始:
祝你好运 您需要的是Apache中的mod_rewrite模块(如果是这样的话,也可以是与IIS等效的模块)和.htaccess文件 如果您有兴趣,请参阅以下文档: 下面是一个示例规则,它应该做您想做的事情(将anything.php重定向到index.php?id=anything):
谷歌为apache mod_重写。其他服务器(如IIS)也有自己的模块来重写URL
RewriteRule ^(.*)\.php$ index.php?id=$1 [QSA,L]