【问题标题】:.htaccess on host gator is not removing .php extension and adding trailing slash主机 gator 上的 .htaccess 没有删除 .php 扩展名并添加斜杠
【发布时间】:2019-11-02 11:09:40
【问题描述】:

所以我最近将我的新网站上传到了 hostgator。

我正在尝试删除 .php 扩展名并在我的所有网址中添加一个斜杠。

例如

website.com/about.php 应更改为 website.com/about/

我有在不同主机上的其他网站上工作的 htaccess 代码,但是当我在 hostgator 上为这个网站尝试它时它不起作用。

这是我试过的代码:

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]

目前我的 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 为我添加了这个 htaccess,它基本上将 http 请求重定向到 https 并将 http://www.website.co.uk 和 www.website.co.uk 删除到 website.co.uk

如何删除 .php 扩展名并强制使用斜杠?

【问题讨论】:

    标签: php .htaccess


    【解决方案1】:

    您可以将站点根目录 .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]
    

    确保在新浏览器中或在清除浏览器缓存后进行测试。

    【讨论】:

    • 你好,这确实有效,但是,它也做了太多的重定向,所以如果我在网络检查器中正确加载我的页面后,它会再次加载我的文件,如下所示:website.com /web-page.php.php.php.php.php.php 等
    • 在浏览器中尝试我更新的规则。如果您仍然看到重定向,则在 Chrome 开发工具中测试 缓存禁用 并在“网络”选项卡中检查您获得的 301/302 重定向 URL 是什么。
    • 我只是得到了 301 个重定向,甚至图像都添加了 .php.php.php.php 等
    • 清除浏览器缓存并重新启动后再次尝试我更新的规则。
    • 这就是我现在得到的,它再次起作用并添加了斜杠,但是它尝试添加斜杠的一些文件不正确,它还让我转到 website.com//只是 website.com - ibb.co/YXWmxs5
    【解决方案2】:

    对于初学者,根据您的托管环境,您可能需要将以下内容添加到 htaccess 文件的开头:

    Options -MultiViews
    

    如果 MultiViews 在服务器上,将进行隐式文件名匹配。因此,如果您请求 page/,它可以将其解释为 page.php。使用上面的选项禁用多视图,看看是否有帮助。

    【讨论】:

    猜你喜欢
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    • 2012-04-15
    • 2014-03-04
    • 2017-07-14
    • 1970-01-01
    相关资源
    最近更新 更多