簡體   English   中英

mod_rewrite最簡單的任務:重定向到.htaccess中的index.php

[英]Simplest task ever with mod_rewrite : redirection to index.php in .htaccess

我剛剛使用byet.com免費托管創建並上傳了一個網站。 一切正常,只是在瀏覽器中鍵入純域名不會將用戶重定向到我的index.php文件(而是顯示廣告頁面)。 在谷歌搜索mod_rewrite並瀏覽了十幾個不同的頁面之后,我嘗試在我的.htaccess文件中添加以下內容:

DirectoryIndex index.php

要么

RedirectMatch ^/$ /index.php

或者(從此Stackoverflow 答案

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js)$
RewriteRule .* /index.php [L,R=302]

這些都沒有改變任何內容,或者有時導致我的index.php頁面無法訪問,並顯示錯誤消息“腳本無法正確重定向”。 也許這種行為是在httpd.conf文件的級別上實現的(我無法訪問它),所以我永遠無法改變它。 我問了byte.com的支持中心有關它,但不幸的是,回答我的人似乎並不真正理解英語或我的問題。

任何建議贊賞。

如果你的index.php在你的tld下(例如http://domain.com/index.php ),你可以試試這個;

RewriteEngine On
RewriteRule ^.*$ /index.php [L]

否則,如果index.php在子文件夾中(例如http://domain.com/my/folder/index.php ),則必須添加RewriteRule的路徑;

RewriteEngine On
RewriteRule ^.*$ /my/folder/index.php [L]

如果這不起作用,主機有可能已禁用mod_rewrite模塊(盡管你得到的重定向錯誤信息也是如此)。 您是否檢查過apache的error_log文件以獲取相關消息?

在root .htaccess中嘗試此規則:

RewriteEngine on

RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js)$ [NC]
RewriteRule ^(?!index\.php).*$ /index.php [L,NC,R=302]

暫無
暫無

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

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