2012-08-10 161 views
0

我已經看過每個問題和每個Apache的服務器reWriteRule的例子......沒有任何工作。mod_rewrite乾淨的URL不工作

我只需要改變 http://beta.EXAMPLE.com/profile.php?profile_name=ntgCleanerhttp://beta.EXAMPLE.com/ntgCleaner

我必須看到,如果my .htaccess file is being read,並查看if mod_rewrite is enabled,他們都完全正常工作。

出於某種原因,我遇到的例子不適合我。以下是一些例子。

RewriteEngine On 
RewriteRule /(.*)$ /profile.php?username=$1 

RewriteEngine On 
RewriteBase/
RewriteRule ^profile\/(.*)$ ./profile.php?profileId=$1 [L,NC,QSA] 

Options +FollowSymlinks 
RewriteEngine on 
RewriteOptions MaxRedirects=10 
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?profile_name=$1 
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?profile_name=$1 

但所有這些工作。這是因爲我使用的是一個子域嗎?我計劃在完成網站時最終將BETA中的子域名切換到www。

有什麼建議嗎?

回答

1

試試這個:

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([a-zA-Z0-9_-]+)/?$ /profile.php?profile_name=$1 [L,QSA] 

你的例子有很多不同的是查詢字符串的,所以我不知道哪一個是你真正想要的。您有?username=,?profileId=,?profile_name=

+0

噢,對不起,我只是希望它使用PROFILE_NAME作爲清潔網址,所以從example.com/ profile.php?profile_name = ntgCleaner to example.com/ntgCleaner 那些我們只是我想要的例子 – ntgCleaner 2012-08-10 18:28:32

+0

這正是我需要的!我不認爲request_filename必須處理任何事情......謝謝! – ntgCleaner 2012-08-10 18:30:56

+1

@ntgCleaner這是一種不會無限循環的方式,因爲如果URI指向現有資源(如profile.php),它將不會重新重寫。 – 2012-08-10 18:33:46

0

您必須使用條件(RewriteCond),當條件滿足時,應用規則(RewriteRule)。所以,當你不使用條件時,不會使用規則,因爲引擎不知道該使用什麼。

的RewriteCond重寫規則必須之前使用,我以這種形式用它對付網絡機器人:

RewriteCond %{REMOTE_HOST} .googlebot.com$ [NC,OR] 
RewriteCond %{REMOTE_HOST} .search.msn.com$ [NC,OR] 
RewriteCond %{REMOTE_HOST} .kimsufi.com$ [NC] 
RewriteRule ^.*$ /errors/404.html [F] 
+0

偉大的信息,謝謝你爲我清除drobik! – ntgCleaner 2012-08-10 18:39:09