5 .htaccess重写:强制HTTPS,删除index.php,删除.php,强制www,强制尾随斜杠 [英] 5 .htaccess Rewrites: Force HTTPS, Remove index.php, Remove .php, Force www, Force Trailing Slash

查看:211
本文介绍了5 .htaccess重写:强制HTTPS,删除index.php,删除.php,强制www,强制尾随斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了很多文章,似乎无法将所有合并后的.htaccess重写一起工作。我要么重新直接循环,要么一个或几个完全不起作用。

I've read many articles and cannot seem to get ALL of the combined .htaccess Rewrites to work together. I either get re-direct loops or one or a few do not work at all.

要清楚我正在寻找以下5个,如果需要的话:

To be clear I'm looking for the following 5 to occur if needed:


  • 确保所有网址

  • 确保HTTPS 适用于所有版本的网站

  • 从网址中删除index.php

  • 删除所有.php扩展程序 /重定向到没有.php扩展名的网址

  • 可以与上一个一起:添加尾随斜杠

  • Ensure www on all URLs
  • Ensure HTTPS for all version of site
  • Remove index.php from url
  • Remove all .php extension / Re-direct to url without .php extension
  • Could be in tandem with the previous: add trailing slash

一些例子:

  • example.com/index.php => https://www.example.com/
  • www.example.com/info/really-good-info.php => https://www.example.com/info/really-good-info/
  • www.example.com/about-us.php => https://www.example.com/about-us/
  • www.example.com/careers/index.php => https://www.example.com/careers/

这是当前.htaccess设置:

Here is current .htaccess setup:

<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /

# Remove all .php extensions without interfering with .js or .css.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.(?!js|css)([^.]*)$ $1\.php

# Remove index from url.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index?$1 [L,QSA]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]

# Ensure www on all URLs.
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

# Ensure we are using HTTPS version of the site.
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Ensure all URLs have a trailing slash.
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ https://www.example.com/$1/ [L,R=301]

</IfModule>

以上.htaccess仅在我的根文件夹中,目前需要5个中的3个:更改到HTTPS,添加www并删除index.php。它不会删除任何其他.php文件扩展名,也不会添加尾部斜杠。

The above .htaccess is ONLY in my root folder and currently does 3 out of the 5 needed: changes to HTTPS, adds www and removes index.php. It does not remove any other .php files extension nor does it add trailing slash.

推荐答案

我看到2个问题:

在重写之后出现的重定向规则
添加 .php 只应在确保相应的 .php 文件存在。

Redirect rules appearing after rewrite ones Adding .php should only happen after you ensure corresponding .php file exists.

这样做:

Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /

# Ensure www on all URLs.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=302]

# Ensure we are using HTTPS version of the site.
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]

RewriteCond %{THE_REQUEST} \s/*(.*?)/index\.php [NC]
RewriteRule ^ %1/ [R=302,L]

RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L]

# Ensure all URLs have a trailing slash.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^.]*?[^/.]$ %{REQUEST_URI}/ [L,R=302]

# Remove all .php extensions without interfering with .js or .css.
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]

# Remove index from url.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+?)/?$ index.php?$1 [L,QSA]

确保在测试之前清除浏览器缓存。

Make sure to clear your browser cache before testing this.

这篇关于5 .htaccess重写:强制HTTPS,删除index.php,删除.php,强制www,强制尾随斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆