2010-09-22 47 views
1

一盞燈的服務器,我想要的網址在LAMP服務器上,我希望將URL http://example.com/index.php重寫爲http://example.com。我怎麼做?

http://example.com/index.php
重寫簡單地
http://example.com

我現在的.htaccess文件如下...

IndexIgnore * 

ErrorDocument 400 /index.php?module=error&action=error 
ErrorDocument 401 /index.php?module=error&action=error 
ErrorDocument 403 /index.php?module=error&action=error 
ErrorDocument 404 /index.php?module=error&action=error 
ErrorDocument 500 /index.php?module=error&action=error 

RedirectMatch 301 ^/media/$/
RedirectMatch 301 ^/media/documents/$/
RedirectMatch 301 ^/media/graphics/$/
RedirectMatch 301 ^/media/photos/$/
RedirectMatch 301 ^/library/$/
RedirectMatch 301 ^/library/css/$/
RedirectMatch 301 ^/library/ht/$/
RedirectMatch 301 ^/library/js/$/
RedirectMatch 301 ^/library/php/$/

RewriteEngine on 
RewriteBase/

RewriteRule ^home$ /index.php?module=home&action=frontpage 
RewriteRule ^home/$ /index.php?module=home&action=frontpage 
RewriteRule ^home/([^/\.]+)$ /index.php?module=home&action=$1 
RewriteRule ^home/([^/\.]+)/$ /index.php?module=home&action=$1 

RewriteRule ^cv$ /index.php?module=home&action=cv 
RewriteRule ^cv/$ /index.php?module=home&action=cv 

RewriteRule ^release$ /index.php?module=release&action=release 
RewriteRule ^release/$ /index.php?module=release&action=release 

RewriteRule ^photos$ /index.php?module=gallery&action=album&album=general 
RewriteRule ^photos/$ /index.php?module=gallery&action=album&album=general 

RewriteRule ^gallery$ /index.php?module=gallery&action=album&album=general 
RewriteRule ^gallery/$ /index.php?module=gallery&action=album&album=general 
RewriteRule ^gallery/([^/\.]+)$ /index.php?module=gallery&action=album&album=$1 
RewriteRule ^gallery/([^/\.]+)/$ /index.php?module=gallery&action=album&album=$1 
RewriteRule ^gallery/([^/\.]+)/([^/\.]+)$ /index.php?module=gallery&action=album&album=$1$&page=$2 
RewriteRule ^gallery/([^/\.]+)/([^/\.]+)/$ /index.php?module=gallery&action=album&album=$1$&page=$2 
RewriteRule ^gallery/([^/\.]+)/([^/\.]+)/([^/\.]+)$ /index.php?module=gallery&action=item&album=$1$&page=$2&item=$3 
RewriteRule ^gallery/([^/\.]+)/([^/\.]+)/([^/\.]+)/$ /index.php?module=gallery&action=item&album=$1$&page=$2&page=$3 

RewriteRule ^handouts$ /index.php?module=home&action=handouts 
RewriteRule ^handouts/$ /index.php?module=home&action=handouts 

RewriteRule ^links$ /index.php?module=home&action=links 
RewriteRule ^links/$ /index.php?module=home&action=links 

RewriteRule ^contact$ /index.php?module=home&action=contact 
RewriteRule ^contact/$ /index.php?module=home&action=contact 

RewriteRule ^login$ /index.php?module=authentication&action=login 
RewriteRule ^login/$ /index.php?module=authentication&action=login 
RewriteRule ^logout$ /index.php?module=authentication&action=logout 
RewriteRule ^logout/$ /index.php?module=authentication&action=logout 

RewriteRule ^copyright$ /index.php?module=home&action=copyright 
RewriteRule ^copyright/$ /index.php?module=home&action=copyright 

RewriteRule ^error$ /index.php?module=error&action=error 
RewriteRule ^error/$ /index.php?module=error&action=error 

我應該如何修改我的.htaccess文件來實現這個基本重寫?此外,任何其他反饋與我的.htaccess代碼將不勝感激。

在此先感謝!

+0

我從來沒有見過這樣一個濃密的.htaccess文件在我的生活中。你真的寫了所有的亂七八糟,不知道如何添加這個簡單的重寫規則? – 2010-09-22 13:59:15

+0

我寫過,哈哈,但是我遇到了重定向循環錯誤的問題。也許它太濃密了!大聲笑任何關於如何使代碼更有效的建議? – Drew2345 2010-09-22 14:33:39

回答

0
RewriteRule ^/index.php$/

但是這沒有任何意義,因爲/可能會成爲index.php。 如果你的目錄中有一個index.php文件,並且不想將它作爲默認文件提供,那麼它就是一個非常奇怪的配置!但當然可能...如果你已經在你的web服務器配置中指定了不同的DirectoryIndex'es。

也許你想重定向index.php/ ???

在這種情況下,你可以把一個RedirectMatch在你的配置一樣RedirectMatch 301 ^/index.php$ /,但我寧願建議這樣做在你的PHP文件直接看着你的$_SERVER["REQUEST_URI];但是這是一個風格問題。我個人比較喜歡有儘可能多的控制,我的應用程序如果可能的話,只移動到服務器配置如果它的速度更快或有必要...

編輯:

後您的評論它清除你真正需要的,我可以給你兩個解決方案。

Redirect/RedirectMatch將無法​​正常工作,因爲您無法在條件下執行此操作,您可以在此檢查實際的請求uri。此外,最終投放的網址將用於重定向。這意味着在通過DirectoryIndex指令由apache重定向到index.php之後。 所以這些方法將無法區分//index.php之間的區別。

,所以你需要做的是無論是在你的PHP文件,該文件是

版本1:

看看$_SERVER['REQUEST_URI']index.php結束,如果它的實際請求,這隻會發生(輸入到瀏覽器欄) 。那裏你可以使用header("Location: ...")做一個redreict。

版本2:

使用mod rewrite這也是能夠做到的重定向和可以做到這一點的條件。 包含您擁有的配置文件(DirectoryIndex)用於演示目的。 您實際上只需要RewriteCondRewriteRule一行。

DirectoryIndex index.php 

RewriteEngine On 
RewriteBase/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/ 
RewriteRule ^index\.php$ http://www.yourdomain.com/ [R=301,L] 

我不知道wheter其可能離開你的域,並只需鍵入/,你可以看看這件事。 只有當請求url實際上是/index.php時,纔會應用重定向的重寫。

+0

我認爲你是正確的,我正在尋找重定向從index.php到/但是,使用RedirectMatch 301 ^/index.php $ /導致重定向循環頁面加載錯誤。有任何想法嗎? – Drew2345 2010-09-22 14:15:29

+0

是的,我會將它添加到答案的一個時刻... – 2010-09-22 14:29:24

+0

我使用的版本2,它完美的作品。非常感謝!值得注意的是,(1)DirectoryIndex index.php行似乎並不必要,並且(2)簡單地替換http://www.yourdomain.com/ /似乎工作正常。 – Drew2345 2010-09-22 14:45:19

相關問題