2013-10-23 59 views

回答

1

歡迎來到SO。如果您在Apache網絡服務器上運行您的網站,則可以使用Apache Module mod_rewrite來實現此目的。這裏有一個.htaccess文件的例子,你必須放置在你的網站的根目錄下。

# switch on mod_rewrite 
RewriteEngine On 

# redirect all non-www request to www 
RewriteCond %{HTTP_HOST} !^$ 
RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteCond %{HTTPS}s ^on(s)| 
RewriteRule^http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

# rewrite all physical files and folders 
RewriteCond %{REQUEST_FILENAME} !-f [OR] 
RewriteCond %{REQUEST_FILENAME} !-d 

# add all exceptions that should not be redirected 
RewriteCond %{REQUEST_URI} "/layout/" [OR] 
RewriteCond %{REQUEST_URI} "/javascript/" [OR] 
RewriteCond %{REQUEST_URI} "/content" 

# and finally pass everything to the index.php as a parameter 
RewriteRule .* - [L] 
RewriteRule (.*) index.php?value=$1 [QSA] 

這將做到以下幾點:

  • 如果用戶請求http://domain.com他將forwared到http://www.domain.com
  • 請求domain.com/hello/world.html將以index.php文件結尾,保存爲hello/world.html的現有$_GET['value'],然後可以進一步處理
+0

工作,但顯示相同的每個頁面的回聲需要不同的PHP回聲根據URL –

+0

@ kritprim對不起,我不太瞭解你的評論。你的意思是,你需要爲每個請求使用不同的PHP文件,比如'/ hello'會被重定向到'hello.php'? – insertusernamehere

+0

php文件的回聲urlof currunt頁面 –

相關問題