.htaccess-如何修改,以便重定向到目录URL(不带斜杠),但仍从指定目录加载index.php

时间:2019-06-14 12:51:10

标签: apache .htaccess

我的.htaccess文件中包含以下代码。它将非www重定向到www,从URL中删除斜杠,从URL中删除.php,并从目录URL加载index.php而没有后斜杠。一切都很好。

问题是,当我键入“ www.notmysite.co.uk/hello/index”时,它不会删除链接的“ / index”部分并进行重定向。我该如何修改代码才能做到这一点?

例如:

www.notmysite.co.uk/articles/index->​​ www.notmysite.co.uk/articles(无效)

www.notmysite.co.uk/articles/index.php-> www.notmysite.co.uk/articles(工作中)

www.notmysite.co.uk/articles/-> www.notmysite.co.uk/articles(工作中)

www.notmysite.co.uk/contact.php-> www.notmysite.co.uk/contact(工作中)

在(无效)示例中,它确实加载了页面,只是没有重定向到正确的网址。

DirectorySlash Off

RewriteEngine On
RewriteBase /

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

# Remove all trailing slash if any
RewriteRule ^(.*)/$ /$1 [R=301,L]

# Redirect all .php extension to no extension
RewriteCond %{THE_REQUEST} .+\.php [NC]
RewriteRule ^(.*?)(\/index)?\.php$ $1 [R,L]

# Remove .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

# Load index page when url is directory
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ $1/index.php [NC,L]

0 个答案:

没有答案