2015-12-29 17 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 

希望这段代码会为你:)