没有文件结尾的htaccess域访问

时间:2018-04-11 14:58:21

标签: .htaccess

我希望domain.de/test.php不带.php。 我尝试过这个,但它不起作用。可能有冲突吗?

这是我现在的全部.htaccess。我在某处有错误或有冲突吗?

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

# 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

<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 ##

我尝试将带有文件扩展名的部分放在顶部和底部,但是到处都不起作用。

2 个答案:

答案 0 :(得分:1)

您可以在站点根目录中使用这些规则.htaccess:

RewriteEngine On

# remove www and redirect http to https
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]

# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]

# internally add .php extension 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule ^(.+?)/?$ $1.php [L]

答案 1 :(得分:0)

解决方案是添加RewriteBase /

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]