2017-05-28 58 views
0

嘿,我想知道人们如何制作网站是动态的,他们可以在网站中使用像/ user/kappa这样的URL,而不需要在文件中包含实际的文件夹。通过php的自定义目录

我不太确定他们是如何做到这一点,但我一直在寻找一段时间,找不到一个例子,所以我想我可能会问这里!

https://domain.tld/user/kappa

喜欢它如何说/用户/卡帕,但不会使文件夹物理 是有办法,我可以做到这一点使用PHP?

任何帮助,将不胜感激。

+0

阿帕奇'mod_rewrite' –

+0

你可以,如果你使用的是Apache服务器使用htaccess的和重定向 –

回答

0

要达到此目的,您可以使用mod_rewrite

您可以添加几行到您的.htaccess文件进行设置。它正在做的是采用一组包含变量的PHP文件,并根据您制定的规则重写URI。例如,你可能有一个index.php文件用不同的变量处理你的所有内容。即:

example.com/index.php?page=news&id=34 

你可以使用国防部重写来改变这

example.com/news/title.html 

举个例子,这是一些代码的CMS的Joomla用于其网址。我也发现了这个页面,其中包含一些基本的例子。 http://www.the-art-of-web.com/system/rewrite/1/

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]