[英].htaccess on host gator is not removing .php extension and adding trailing slash
So I have recently uploaded my new website to hostgator.所以我最近将我的新网站上传到了 hostgator。
I am trying to remove.php extension and add a trailing slash to all my urls.我正在尝试删除.php 扩展名并在我的所有网址中添加一个斜杠。
For example例如
website.com/about.php should change to website.com/about/ website.com/about.php 应更改为 website.com/about/
I have the htaccess code that has worked for other websites on different hosts, but when i tried it for this website on hostgator it doesnt work.我有适用于不同主机上的其他网站的 htaccess 代码,但是当我在 hostgator 上为这个网站尝试它时它不起作用。
This is the code I tried:这是我试过的代码:
RewriteEngine On
RewriteBase /
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://website.co.uk/$1 [R,L]
RewriteCond %{HTTP_HOST} ^www.website.co.uk [NC]
RewriteRule ^(.*)$ http://website.co.uk/$1 [L,R=301]
Currently I now have this in my htaccess:目前我现在在我的 htaccess 中有这个:
RewriteEngine On
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]
# remove www and turn on https in same rule
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
Hostgator added this htaccess for me which basically redirects http requests to https and removes http://www.website.co.uk and www.website.co.uk to website.co.uk Hostgator 为我添加了这个 htaccess,它基本上将 http 请求重定向到 https 并删除http://www.co.uk和 www.website。
What can I do to remove.php extension and force trailing slash?我该怎么做才能删除.php 扩展名和强制斜杠?
You can replace all the code in your site root .htaccess with this:您可以将站点根目录 .htaccess 中的所有代码替换为:
RewriteEngine On
# remove www and turn on https in same rule
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(.*?)/?$ https://%1/$1/ [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]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301,NE]
# To internally rewrite /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]
Make sure to test this in a new browser or after clearing your browser cache.确保在新浏览器中或在清除浏览器缓存后进行测试。
For starters, depending on your hosting environment you may need to add the following to the beginning of your htaccess file:对于初学者,根据您的托管环境,您可能需要将以下内容添加到 htaccess 文件的开头:
Options -MultiViews
If MultiViews is on the server will do implicit filename matching.如果 MultiViews 在服务器上,将进行隐式文件名匹配。 So if you request page/ it could interpret that as page.php.
因此,如果您请求 page/,它可以将其解释为 page.php。 Use the option above to disable MultiViews and see if that helps.
使用上面的选项禁用 MultiViews 并查看是否有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.