.htaccess重定向.php和.html请求

时间:2015-12-29 10:20:23

标签: .htaccess silverstripe

我使用名为SilverStripe的框架...我们目前正在将旧网站迁移到此框架。问题是旧站点URL以.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]

5 个答案:

答案 0 :(得分: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

第四行从网址中删除.html / .php

答案 1 :(得分: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]

答案 2 :(得分:3)

以下是问题的简短解决方案

请尝试以下规则:

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

这将重写

/file.php OR /file.html

   /framework/main.php?url=$1

模式解释:

^([^。] +)。(html | php)$ 匹配以任何字符开头的任何请求,这些字符不包括点后跟一个littrel dot char,后跟文字php或html结尾uri。

注意:这应该是你的htaccess中任何其他规则之前的第一条规则。

答案 3 :(得分:3)

试试这段代码: -

#app/views/layouts/application.html.erb
<%= stylesheet_link_tag :application, "http://cdn.google.com/stylesheet.css" %>

希望此代码适合您:)

答案 4 :(得分:0)

基本......

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

编辑现在您已经提到过您有数百个......上面的答案仍然有效,因为您可以将数百个这些添加到htaccess(我已经看过了)它很多次......但是很可能也像这样......

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

现在你可能有一个特定的html文件,你想要允许访问仍然存在,并且需要添加作为此规则的例外。