簡體   English   中英

將所有請求重定向到index.php而不使用.htaccess

[英]Redirect all requests to index.php WITHOUT .htaccess

我目前正在使用以下命令將所有請求重定向到index.php文件:

<IfModule mod_rewrite.c>
RewriteEngine on

# base path, if not the root directory
RewriteBase /dev/public/

# direct all requests to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php
</IfModule>

在這種情況下,它將我的代碼限制為只能在啟用了mod_rewrite的服務器上運行。 所以我想知道,有沒有一種方法可以在不使用.htaccess的情況下模擬這種行為? 其他框架如何做?

您可以編寫一個簡短的php腳本:

<?php
    header( 'Location: index.php' ) ;
?>

並將其包含在除index.php之外的所有PHP文件的頂部

如果您不想使用htaccess,則可以使您的所有鏈接都像index.php?c=products&m=view&id=345這樣您的所有請求都將以任何方式發送到index.php,而您不需要htaccess

像Yahoo這樣的主機不允許使用.htaccess,這可能會令人沮喪。 據我所知,這樣的重定向是不可能的,但這是一種解決方法:

具有以下文件結構:

index.php
private/ <-- you could do: password protect, set permissions, name with hard to guess string, etc
public/

因此,訪問example.com/page將輸出404,但是您可以將URL重做到example.com/index.php/page這是我用來在數組中獲取URI args的方法:

$uri_args = 
       explode('/', substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], 'index.php') + 10));

(可選)刪除空的args: array_filter($uri_args);

看到我的問題: 如何僅在PHP中重寫URL?
(另請參見http://php.net/manual/en/reserved.variables.server.php和“ 刪除空數組元素”

暫無
暫無

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

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