2015-12-29 62 views
8

我使用名爲SilverStripe的框架......我們目前正在將舊網站遷移到此框架中。問題在於舊網站的網址以.php或.html結尾,而在新網站中卻沒有。.htaccess重定向.php和.html請求

我需要修改第二個重寫規則,以便將請求傳送到main.php而不帶任何.html或.php擴展名。

在我目前的.htaccess我有以下規則:

# Turn off index.php handling requests to the homepage fixes issue in apache >=2.4 
<IfModule mod_dir.c> 
    DirectoryIndex disabled 
</IfModule> 

SetEnv HTTP_MOD_REWRITE On 
RewriteEngine On 

# Enable HTTP Basic authentication workaround for PHP running in CGI mode 
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] 

# Deny access to potentially sensitive files and folders 
RewriteRule ^vendor(/|$) - [F,L,NC] 
RewriteRule silverstripe-cache(/|$) - [F,L,NC] 
RewriteRule composer\.(json|lock) - [F,L,NC] 

# Process through SilverStripe if no file with the requested name exists. 
# Pass through the original path as a query parameter, and retain the existing parameters. 
RewriteCond %{REQUEST_URI} ^(.*)$ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule .* framework/main.php?url=%1 [QSA] 

# If framework isn't in a subdirectory, rewrite to installer 
RewriteCond %{REQUEST_URI} ^(.*)/framework/main.php$ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule . %1/install.php? [R,L] 

可能的解決方法(仍在測試):

RewriteCond %{REQUEST_URI} ^(.*)\.html [OR] 
RewriteCond %{REQUEST_URI} ^(.*)\.php [OR] 
RewriteCond %{REQUEST_URI} ^(.*)$ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule .* framework/main.php?url=%1 [QSA] 

回答

4

將下列規則添加到您的.htaccess文件的規則# Deny access to potentially sensitive files and folders塊如下:

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI} ^(.+)\.html$ 
RewriteRule (.*)\.html$ /$1 [R=301,L] 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI} ^(.+)\.php$ 
RewriteRule (.*)\.php$ /$1 [R=301,L] 

前兩行確認網址是不是一個目錄,而不是文件。

第三行檢查url是否包含.html.php

第四線從URL

+0

@ 3dogo不幸的是規則沒有觸發 –

+0

這很奇怪。它適用於我的測試。例如,如果我訪問「www.example.com/about.html」,它將重定向到「www.example.com/about」。另一個例子「www.example.com/about/team.php」將重定向到「www.example.com/about/team」。 – 3dgoo

+0

你用什麼版本的Apache進行測試? –

0

怎麼樣的基本.. 。

Redirect 301 /about-us.html http://www.domain.com/about-us 

編輯現在你提到你有數百個這樣......上面的回答仍然是,你可以添加數百個這樣的htaccess的有效(我見過很多次了)......但是它很可能也是這樣......

RedirectMatch 301 (.*)\.html$ http://www.domain.com$1/ 

不這樣做一行一行的缺點現在是,你可能有你想允許訪問還是一個特定的HTML文件,並且將需要增加作爲此規則的例外。

+0

工作,因爲我有大約900這樣的網址:) –

+0

它確實回答了他們的問題,只是發生了,他們現在提供了新的信息! – Barry

+0

是否有可能改變規則...... RewriteRule。* framework/main.php?url =%1 [QSA]這樣,傳遞給那個url參數的東西永遠不會有.html或.php擴展名? –

3

這裏刪除.html/.php只有很短的解決問題的辦法

嘗試以下規則:

RewriteRule ^([^.]+)\.(html|php)$ /framework/main.php?url=$1 [NC,L,QSA] 

這將改寫

/file.php OR /file.html 

/framework/main.php?url=$1 

圖案解釋:

^([^。] +)(HTML | PHP)$任何請求匹配開始後跟一個littrel點炭,接着字面PHP不含點的任何字符。或uri結尾的html。

注意:這應該是您的htaccess之前的任何其他規則的第一條規則。

4

您只需調整現有的規則有點:

# Turn off index.php handling requests to the homepage fixes issue in apache >=2.4 
<IfModule mod_dir.c> 
    DirectoryIndex disabled 
</IfModule> 

SetEnv HTTP_MOD_REWRITE On 
RewriteEngine On 

# Enable HTTP Basic authentication workaround for PHP running in CGI mode 
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] 

# Deny access to potentially sensitive files and folders 
RewriteRule ^vendor(/|$) - [F,L,NC] 
RewriteRule silverstripe-cache(/|$) - [F,L,NC] 
RewriteRule composer\.(json|lock) - [F,L,NC] 

# Process through SilverStripe if no file with the requested name exists. 
# Pass through the original path as a query parameter, and retain the existing parameters. 
# Strip out .html or .php from request URI 
RewriteCond %{REQUEST_URI} ^(.+?)(?:\.(?:html|php))?$ [NC] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule^framework/main.php?url=%1 [L,QSA] 

# If framework isn't in a subdirectory, rewrite to installer 
RewriteCond %{REQUEST_URI} ^(.*)/framework/main\.php$ [NC] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule . %1/install.php? [R,L] 
3

試試這個代碼: -

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.html -f 
RewriteRule ^(.*)$ $1.html 

希望這段代碼會爲你:)