簡體   English   中英

在LAMP服務器上,我希望將URL http://example.com/index.php重寫為http://example.com。 我怎么做?

[英]On a LAMP server, I want the URL http://example.com/index.php to be rewritten to http://example.com. How do I do that?

在LAMP服務器上,我想要URL

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代碼的任何其他反饋將不勝感激。

提前致謝!

RewriteRule ^/index.php$ /

但這沒有意義,因為/可能會提供index.php 如果您的目錄中有一個index.php文件,並且不想將其作為默認文件,則它的配置非常奇怪! 但當然可以...如果您在網絡服務器配置中指定了不同的DirectoryIndex

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

在這種情況下,您可以像配置RedirectMatch 301 ^/index.php$ /一樣在您的配置中放置一個RedirectMatch ,但我寧願建議您直接在$_SERVER["REQUEST_URI];直接在您的php文件中這樣做$_SERVER["REQUEST_URI];但這是一個問題。風格,我個人希望盡可能在應用程序中擁有控制權,並且僅在速度更快或必要時才移至服務器配置...

編輯:

在您的評論清除了您的實際需求之后,我可以給您兩個解決方案。

Redirect / RedirectMatch無法正常工作,因為您不能有條件地這樣做,您可以在其中檢查實際的請求uri。 另外,最終提供的網址將用於重定向匹配。 這意味着在Apache通過DirectoryIndex指令重定向到index.php之后。 因此這些方法將無法分辨//index.php之間的區別。

所以您需要在您的php文件中執行此操作

版本1:

看看$_SERVER['REQUEST_URI']index.php結尾,這只有在其實際請求(在瀏覽器欄中鍵入)時才會發生。 在那里,您可以使用header("Location: ...")

版本2:

使用mod rewrite ,它也能夠進行重定向,並且可以在一定條件下進行重定向。 包括用於演示的configuratino( 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]

我不確定是否可以離開您的域,只需鍵入/ ,您就可以查找。 僅當請求url實際上是/index.php應用重寫器進行重定向。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM