将.php和.html重定向到无扩展版本

时间:2018-04-15 05:43:54

标签: .htaccess

我有像domain.de/test这样的页面,我希望每个其他版本都重定向到这个:

  • www.domain.de/test
  • 列出itemdomain.de/test.php
  • domain.de/test/
  • (及其混合形式)

我该如何解决这个问题?除了删除文件扩展名外,我找到了每个版本的解决方案。但是,图像和pdf文件扩展名当然应该有效。

这是我的代码:

RewriteEngine on
RewriteBase /
# FORCE HTTPS
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://bm-translations.de/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

#remove the need for .php extention 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . / [L,R=301]

<IfModule mod_filter.c>
<IfModule mod_deflate.c>
## Enable gzip compression ##
# compress text, HTML, JavaScript, CSS, and XML
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

# Remove browser bugs
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
## END Enable gzip compression ##
</IfModule>
</IfModule>

AddType font/woff .woff
ExpiresByType font/woff "access plus 1 year"
AddOutputFilterByType DEFLATE font/woff

AddType image/svg+xml .svg
AddOutputFilterByType DEFLATE image/svg+xml

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 3 month"
ExpiresByType image/jpg "access 3 month"
ExpiresByType image/jpeg "access 3 month"
ExpiresByType image/gif "access 3 month"
ExpiresByType image/png "access 3 month"
ExpiresByType image/svg+xml "access 3 month"
ExpiresByType text/css "access 3 month"
ExpiresByType text/html "access 3 month"
ExpiresByType application/pdf "access 3 month"
ExpiresByType text/x-javascript "access 3 month"
ExpiresByType application/javascript "access plus 3 month"
ExpiresByType application/x-shockwave-flash "access 3 month"
ExpiresByType image/x-icon "access 3 year"
ExpiresDefault "access 3 month"
</IfModule>
## EXPIRES CACHING ##

有没有办法将这些规则以某种方式结合起来以减少代码?

1 个答案:

答案 0 :(得分:1)

您可以使用以下htaccess(RewriteRules的较短版本)。我还添加了从网址中删除.php扩展名的规则。

 RewriteEngine on

 #http to https and www to non-www in a single redirect
RewriteCond %{SERVER_PORT} 80 [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ https://bm-translations.de/$1 [L,R=301]
# remove php extension
# redirect/ /file.php to /file
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.php$ /$1 [L,R]
#rewrite /file to /file.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/?$ /$1.php [L]

<IfModule mod_filter.c>
<IfModule mod_deflate.c>
## Enable gzip compression ##
# compress text, HTML, JavaScript, CSS, and XML
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

# Remove browser bugs
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
## END Enable gzip compression ##
</IfModule>
</IfModule>

AddType font/woff .woff
ExpiresByType font/woff "access plus 1 year"
AddOutputFilterByType DEFLATE font/woff

AddType image/svg+xml .svg
AddOutputFilterByType DEFLATE image/svg+xml

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 3 month"
ExpiresByType image/jpg "access 3 month"
ExpiresByType image/jpeg "access 3 month"
ExpiresByType image/gif "access 3 month"
ExpiresByType image/png "access 3 month"
ExpiresByType image/svg+xml "access 3 month"
ExpiresByType text/css "access 3 month"
ExpiresByType text/html "access 3 month"
ExpiresByType application/pdf "access 3 month"
ExpiresByType text/x-javascript "access 3 month"
ExpiresByType application/javascript "access plus 3 month"
ExpiresByType application/x-shockwave-flash "access 3 month"
ExpiresByType image/x-icon "access 3 year"
ExpiresDefault "access 3 month"
</IfModule>
## EXPIRES CACHING ##