2015-11-25 28 views
1

使用WamServer,我用Generate It!過度寫這樣重寫國防部仍返回原始地址

原始URL網址:

http://localhost/CMS/foo.php?id=2&slug=eyeglasses 

的重寫URL:

http://localhost/2/eyeglasses.php 

的規則,網站給我的是

RewriteEngine On 
RewriteRule ^([^/]*)/([^/]*)\.php$ /CMS/foo.php?id=$1&slug=$2 [L] 

但是這仍然是恢復原來的URL

http://localhost/CMS/foo.php?id=2&slug=eyeglasses 

能否請你讓我知道我做錯了什麼?

更新

更明確!我有一個index.php包含了像

echo '<a href="foo.php?id='. $theID .'&slug='. $slug .'" class="list-group-item" data-id="'.$theID.'">'.$theName.'</a>'; 

一個環節,現在我們用戶foo.php登陸網址看起來像

http://localhost/CMS/foo.php?id=1&slug=eyeglassess 

是否正確?我想這樣做是顯示

http://localhost/2/eyeglasses.php 

,而不是原來的URL

+0

請說明。你是否從瀏覽器發送URL'http://localhost/CMS/foo.php?id = 2&slug = eyeglasses',並期待它在瀏覽器地址欄中被重寫爲'http:// localhost/2/eyeglasses .php「,或者你是否從瀏覽器發送了'http:// localhost/2/eyeglasses.php',它將重定向而不是內部重寫爲'http://localhost/CMS/foo.php?id = 2&slug = eyeglasses'?如果第一個問題是你所看到的,那通常不是如何使用mod_rewrite,並且需要幾條額外的線來制定。 –

+0

換句話說,mod_rewrite不能在你的代碼中自動轉換URL或者在野外轉換。它通常用於將乾淨的URL路由到更復雜的應用程序中。 –

+0

感謝邁克爾,我實際上有一個'index.php'頁面,我有鏈接'\t echo''.$theName.'';'現在在foo.php中的'foo.php'而不是'http:// localhost/CMS/foo.php?id = 2&slug = eyeglasses'我想在網址 – Behseini

回答

1

你需要註冊一個重定向規則,舊網址重定向到URL漂亮。此代碼應在/CMS/.htaccess

RewriteEngine On 
RewriteBase /CMS/ 

# redirect from actual URL to pretty one 
RewriteCond %{THE_REQUEST} /CMS/+foo\.php\?id=([^\s&]+)&slug=([^\s&]+) [NC] 
RewriteRule^%1/%2.php? [R=302,L,NE] 

RewriteRule ^([^/]+)/([^/]+)\.php$ foo.php?id=$1&slug=$2 [L,NC,QSA] 

但是這是更好地改變你的鏈接代碼,以新的漂亮的鏈接方案。

+0

感謝這正是我正在尋找,但!爲什麼我得到'Not Found'頁面! – Behseini

+0

'http:// localhost/2/eyeglasses.php' – Behseini

+0

有沒有什麼辦法擺脫ID?我的意思是保持它像'http:// localhost/eyeglasses.php'' – Behseini