【问题标题】:mod_rewrite | remove .php and force httpsmod_rewrite |删除 .php 并强制 https
【发布时间】:2020-09-06 17:28:28
【问题描述】:

我在 html/js/php 中创建了一个小的静态网站。

现在我只想要 https:// 非 www。要使用的域(例如:https://zufallsgenerator.org/)。应重定向所有其他组合。目前可以通过各种可能的组合访问该网站...

为了使 URL 更具可读性,我还想删除 .php 扩展名

我只能让其中一项工作。一旦我尝试将它们结合起来,我就会得到太多的重定向。目前只有删除 .php 扩展是在线的。

我用的是众所周知的sn-p:

# 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]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

将 http 重定向到 https 和我通常使用的非 www:

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

RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

任何想法如何组合它们? 有最佳做法吗?

我已经搜索了很多,只找到了包含许多重写规则的巨大 .htaccess 文件...

非常感谢!

【问题讨论】:

    标签: php apache .htaccess redirect mod-rewrite


    【解决方案1】:

    这样吧:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
    
    RewriteCond %{ENV:HTTPS} !on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
    
    # To externally redirect /dir/foo.php to /dir/foo
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
    RewriteRule ^ %1/ [R=301,L,NE]
    
    # add a trailing slash    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule . %{REQUEST_URI}/ [L]
    
    # To internally forward /dir/foo to /dir/foo.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.+?)/?$ $1.php [L]
    

    【讨论】:

      猜你喜欢
      • 2015-06-29
      • 2017-08-31
      • 1970-01-01
      • 1970-01-01
      • 2017-09-29
      • 1970-01-01
      • 1970-01-01
      • 2013-07-27
      • 1970-01-01
      相关资源
      最近更新 更多