2010-09-26 113 views
1

我想設置一個默認的變量自帶一個URLPHP:htacess默認變量

的最後一個查詢的.htaccess文件重定向URL,如下所示:

http://www.server.com/folder/subfolder/index.php?page="some-page-name" 

顯示的URL

http://www.server.com/folder/some-page-name 


如果頁面名稱未設置,如:

http://www.server.com/folder/ 

默認爲「索引」。我可以使用PHP函數header("location:url"),但如果URL不是我想要的,那麼它會在最後顯示「索引」部分。



htacess內容

Options -Indexes 

    <IfModule mod_rewrite.c> 
    RewriteEngine On 
    #RewriteCond %{REQUEST_URI} !^(/index\.php|/img|/js|/css|/robots\.txt|/favicon\.ico) 
    RewriteBase /folder/ 
    RewriteRule ^index\.php$ - [L] 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_URI} !^.*\.css.*$ [NC] 
    RewriteRule ^(.*)$ subfolder/index.php/?page=$1 [L] 
    </IfModule> 

    <IfModule mod_rewrite.c> 
    ErrorDocument 404 /error.html 
    ErrorDocument 403 /error.html 
    </IfModule> 
+0

順便說一句,我真的在您的'.htaccess'中看到'?pageID =',而不是你在問題中提到的'?page ='。 – Lekensteyn 2010-09-27 16:30:17

+0

@Lekensteyn,我的不好?page =。 – Zebra 2010-09-28 09:33:44

回答

1

你不必重定向到的index.php。您可以使用類似:

header('Location: /folder/front-page'); 

如果你只是想http://example.com/folder/顯示你的索引頁,你可以用你的PHP腳本如下:

$requested_page = filter_input(INPUT_GET, 'pageID'); 
$allowed_pages = array('some-page', 'some-page-name'); 
if($requested_page == ''){ 
    // display your index page as ?pageID is not set or empty 
} 
elseif(in_array($requested_page, $allowed_pages)){ 
    // display $requested_page 
} 
else{ 
    // display a 404 Not Found error 
} 
+0

爲了這個工作,他必須做'header('Location:/ folder/index');'作爲「首頁」不是他的默認頁面,「索引」是,並且他特別說「那會在結尾處顯示「索引」部分,如果URL ...我不'想要「。 – Josh 2010-09-26 16:10:57

+0

那時我誤解了他的問題,我以爲他在談論'/index.php?pageID=首頁'。重新回答我的答案。 – Lekensteyn 2010-09-27 16:29:05

+0

非常感謝你! – Zebra 2010-09-28 09:36:15

-1

你的問題太長,沒有閱讀,但是如果你只是想在你的.htaccess文件中設置一個變量並將它傳遞給PHP,那就很容易做到這一點 -

在.htaccess文件(http://httpd.apache .ORG /做CS/2.0/MOD/mod_env.html#SETENV):

SetEnv default_url http://my.url/goes/here 

在你的PHP腳本(http://www.php.net/manual/en/reserved.variables.environment.php):

header('Location: '. $_ENV['default_url']); 

或者,如果你有ErrorDocument處理,你也許可以簡單地發送狀態代碼以及(見第三個選項header()http://us.php.net/manual/en/function.header.php)。

+1

我不認爲這就是他要求的......也許讀他的問題會有幫助嗎? – Josh 2010-09-26 16:08:42

1

嘗試在你的PHP文件中的代碼:

$pageID = 'index'; 

if(isset($_REQUEST['pageID']) && !empty($_REQUEST['pageID'])) 
    $pageID = get_magic_quotes_gpc() ? stripslashes($_REQUEST['pageID']) : $_REQUEST['pageID']; 

// Your code should now use the $pageID variable... 

這將完成設置$pageID爲「指數」的默認值。然後,如果mod_rewrite提供了不同的PageID,$pageID將下注到該值。你的代碼應該使用$pageID的值。

+0

謝謝,非常感謝! – Zebra 2010-09-28 09:37:52