2017-05-08 19 views
0

我想重定向: https://www.example.com/whatever-part-is-coming-after-the-urlhttps://www.example.com/index.php?url=whatever-part-is-coming-after-the-urlHT訪問重定向到單個文件

不改變地址欄的要求。

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

RewriteRule ^(.*)$ index.php?url=$1 

如果我用這個,它爲URL的喜歡https://example.com/foo,但不是https://example.com/foo/bar(所有相對URL,如內容/ styles.css的是現在富/內容/ styles.css中,這讓404錯誤)。

當我添加

[R=301] 

重定向是做正確的,但它顯示了一個「醜陋」的網址。 有沒有辦法兼得?

+0

我很抱歉浪費你的時間。我想我會使用http://stackoverflow.com/questions/3899991/htaccess-rewrite-breaks-relative-paths中的方法 – sprunk

回答

0

使用下面的.htaccess代碼:

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} !=/index.php 
RewriteRule . index.php [L] 

它是什麼,所有的重定向路徑和參數的index.php文件。

即 如果域是example.com

所以http://example.com/test - >請求將被帶到index.php文件,並且即使如果http://example.com/test/another/test

該請求將仍然被帶到路徑index.php文件。

這與laravel等框架使用的代碼完全相同。

在index.php文件中,你可以使用:

$request_uri=parse_url($_SERVER['REQUEST_URI']); 
$path = $request_uri['path']; 

$path變量會給你輸入的路徑用戶。