2012-03-26 21 views
3

我有一個這樣的網址 - www.example.com/blog.php/username,它會帶你到用戶名的博客頁面,但我想使用這樣的網址 - www.example.com/blog/username以獲得相同的文件(blog.php)。請問我需要採取哪些措施來實現這一目標?謝謝你的幫助。使用.htaccess重寫一個簡單的URL

+0

您使用任何框架? – Alex 2012-03-26 13:29:31

+0

沒有框架,只是php – Chibuzo 2012-03-26 13:42:12

+0

網站搜索有什麼問題? – mario 2012-03-26 22:00:19

回答

3

啓用了mod_rewrite和.htacess 。然後將此代碼添加到DOCUMENT_ROOT下的.htaccess中:

Options +FollowSymLinks -MultiViews 
# Turn mod_rewrite on 
RewriteEngine On 
RewriteBase/

# If the request is not for a valid directory 
RewriteCond %{REQUEST_FILENAME} !-d 
# If the request is not for a valid file 
RewriteCond %{REQUEST_FILENAME} !-f 
# If the request is not for a valid link 
RewriteCond %{REQUEST_FILENAME} !-l 
# finally this is your rewrite rule 
RewriteRule ^blog/(.+)$ blog.php/$1 [L,NC] 
4

在你的根direstory名爲.htaccess的創建一個文件,這個過去在:

<IfModule mod_rewrite.c> 
    RewriteEngine on 
    RewriteRule  ^blog/(.*)$  yourpage.php?username=$1 [L] 
</IfModule> 

將採取任何caracters 如果您希望只使用字母和數字的([A-Za-z0-9])

改變 (.*)(*)。
1

刪除文件擴展名:

有兩種方法可以做到這一點。只需查看 請求的簡單方法,如果請求的文件名不存在,那麼它會尋找 帶.php(或.asp或其他)擴展名的文件名。在 .htaccess文件:

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ $1.php [L,QSA] 

現在,如果你去:http://domain/about服務器將它解釋爲 如果你去http://domain/about.php

有意義,但如果我們已經打破了URL 和文件名之間的關係,我們可能會將其分解爲智能。改變 .htaccess文件:

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA] 

來源:戴夫短跑 - >http://davedash.com/2006/07/26/how-to-remove-file-extensions-from-urls/

1

1,您應該安裝MOD-重寫

2你應該確保你的文檔根目錄「AllowOverride All」。虛擬主機配置後普通,你應該找到類似:

<Directory "/your/doc/root/path/"> 
    AllowOverride All 
</Directory> 

3這之後你應該開始重寫引擎,並在你的.htaccess創建重寫規則大衛貝朗格建議:

<IfModule mod_rewrite.c> 
    RewriteEngine on 
    RewriteRule  ^blog/(.*)$  blog.php/$1 [L] 
</IfModule>