htaccess将file.php重定向到/ file /,将非www重定向到www

时间:2018-10-16 07:46:36

标签: apache .htaccess

我写了一条规则,允许我查看.php页面而没有文件扩展名。

例如

/test-page.php被重写为/ test-page /

我还将非www重定向到www。但是,如果我尝试直接访问test-page.php,则不会被重定向-我缺少明显的东西吗?

RewriteEngine On

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

1 个答案:

答案 0 :(得分:0)

选中此一项。这个想法来自4026021

RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite.co.uk [NC]
RewriteRule ^(.*)$ https://www.mysite.co.uk/$1 [L,R=301]
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]