通过php自定义目录

通过php自定义目录,php,routing,Php,Routing,嘿,我想知道人们是如何创建动态网站的,因为他们可以在网站中使用像/user/kappa这样的URL,而不必在文件中包含实际的文件夹 我不太清楚他们是如何做到这一点的,但我已经四处寻找了一段时间,没有找到一个例子,所以我想我可以在这里问一下 https://domain.tld/user/kappa 就像它说的/user/kappa,但并没有实际创建文件夹 有没有一种方法可以使用PHP实现这一点 任何帮助都将不胜感激。要实现这一点,您可以使用mod\u rewrite 您可以在.htaccess文

嘿,我想知道人们是如何创建动态网站的,因为他们可以在网站中使用像/user/kappa这样的URL,而不必在文件中包含实际的文件夹

我不太清楚他们是如何做到这一点的,但我已经四处寻找了一段时间,没有找到一个例子,所以我想我可以在这里问一下

https://domain.tld/user/kappa

就像它说的/user/kappa,但并没有实际创建文件夹 有没有一种方法可以使用PHP实现这一点


任何帮助都将不胜感激。

要实现这一点,您可以使用
mod\u rewrite

您可以在.htaccess文件中添加几行来进行设置。它所做的是获取一个包含一组变量的PHP文件,然后根据您设定的规则重新编写URI。例如,您可能有一个index.php文件,它使用不同的变量处理所有内容。即:

example.com/index.php?page=news&id=34
您可以使用mod rewrite将此更改为

example.com/news/title.html
例如,这是CMS Joomla用于其URL的一些代码。我还找到了这一页,上面有一些基本的例子


apache
mod_rewrite
如果您使用的是apache服务器,则可以使用htaccess和重定向
RewriteEngine On
RewriteRule .* index.php [F]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
RewriteCond %{REQUEST_URI} !^/index\.php
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule .* index.php [L]