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

.htaccess redirect .php and .html requests

我使用一个名为 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]

基础怎么样...

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

EDIT 现在你已经提到你有数百个...上面的答案仍然有效,因为你可以将数百个添加到 htaccess(我已经看过很多次)...不过也很有可能是这样...

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

不逐行执行此操作的缺点是,现在您可能有一个特定的 html 文件,您仍然希望允许访问该文件,并且需要将其添加为该规则的例外。

将以下规则添加到 .htaccess 文件的 # Deny access to potentially sensitive files and folders 规则块下方:

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

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

前两行检查 url 不是目录也不是文件。

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

第四行从 url

中删除 .html / .php

这是问题的简短解决方案

尝试以下规则:

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

这将重写

/file.php OR /file.html

   /framework/main.php?url=

模式解释:

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

注意:这应该是 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.
# 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]

试试这个代码:-

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

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

希望这段代码对你有用:)