2017-05-28 59 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]