如何减少301重定向的次数?
How to reduce the number of 301 redirect?
对于我的网站,我想强制 URL 使用 https,不使用 www 且不使用 .php 扩展名。
为此,我的 htaccess 文件包含以下规则:
RewriteEngine On
#force HTTPS and remove the www
RewriteCond %{SERVER_PORT} 80 [OR]
RewriteCond %{HTTP_HOST} ^www\.jvincent\.fr$ [NC]
RewriteRule ^(.*) https://jvincent.fr/ [QSA,L,R=301]
#To remove the .php extension
RewriteCond %{REQUEST_URI} !(php-script) [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ (.*)\.php [NC]
RewriteRule ^ %1 [R=301,L]
它工作正常。
但是,如果我使用 URL http://www.jvincent.fr/phpinfo.php 访问我的网站,我有两个 301 重定向:
- http://www.jvincent.fr/phpinfo.php 至
https://jvincent.fr/phpinfo.php
- https://jvincent.fr/phpinfo.php 到 https://jvincent.fr/phpinfo
我进行了搜索,但无法找到优化的解决方案,并且在所有情况下都只有一个 301 重定向。
我有办法做到吗?合并唯一 RewriteRule 之前的所有条件?使用环境变量(第一部分标志E)?
感谢您的帮助。
杰克
您可以使用:
RewriteEngine On
#force HTTPS and remove the www
RewriteCond %{SERVER_PORT} 80 [OR]
RewriteCond %{HTTP_HOST} ^www\.jvincent\.fr$ [NC]
RewriteRule ^(.*?)(?:\.php)?$ https://jvincent.fr/ [NC,QSA,L,R=301]
#To remove the .php extension
RewriteCond %{REQUEST_URI} !(php-script) [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ (.*)\.php [NC]
RewriteRule ^ %1 [R=301,L]
对于我的网站,我想强制 URL 使用 https,不使用 www 且不使用 .php 扩展名。 为此,我的 htaccess 文件包含以下规则:
RewriteEngine On
#force HTTPS and remove the www
RewriteCond %{SERVER_PORT} 80 [OR]
RewriteCond %{HTTP_HOST} ^www\.jvincent\.fr$ [NC]
RewriteRule ^(.*) https://jvincent.fr/ [QSA,L,R=301]
#To remove the .php extension
RewriteCond %{REQUEST_URI} !(php-script) [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ (.*)\.php [NC]
RewriteRule ^ %1 [R=301,L]
它工作正常。 但是,如果我使用 URL http://www.jvincent.fr/phpinfo.php 访问我的网站,我有两个 301 重定向:
- http://www.jvincent.fr/phpinfo.php 至 https://jvincent.fr/phpinfo.php
- https://jvincent.fr/phpinfo.php 到 https://jvincent.fr/phpinfo
我进行了搜索,但无法找到优化的解决方案,并且在所有情况下都只有一个 301 重定向。 我有办法做到吗?合并唯一 RewriteRule 之前的所有条件?使用环境变量(第一部分标志E)?
感谢您的帮助。
杰克
您可以使用:
RewriteEngine On
#force HTTPS and remove the www
RewriteCond %{SERVER_PORT} 80 [OR]
RewriteCond %{HTTP_HOST} ^www\.jvincent\.fr$ [NC]
RewriteRule ^(.*?)(?:\.php)?$ https://jvincent.fr/ [NC,QSA,L,R=301]
#To remove the .php extension
RewriteCond %{REQUEST_URI} !(php-script) [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ (.*)\.php [NC]
RewriteRule ^ %1 [R=301,L]