【问题标题】:.htccess 301 redirect for domains, https, www, and file extention.htaccess 301 重定向域、https、www 和文件扩展名
【发布时间】:2016-02-04 05:39:19
【问题描述】:

我有几个域名,最近更新了我的网站以从单个文件 (index.php) 运行。除了没有 .php 之外,请求 URI 相同。我的主机是 GoDaddy linux unlimited(是的...)。

我需要我的 .htaccess 来重定向/重写以下内容

  • 将 domain1.com 重定向到 www.primary.com
  • 将 www.domain1.com 重定向到 www.primary.com
  • 将 domain2.com 重定向到 www.primary.com
  • 将 www.domain2.com 重定向到 www.primary.com
  • 强制 https://
  • 强制 www.
  • 从任何请求中删除 .php(/marketing/cost.php 到 /marketing/cost)
  • 删除尾部斜杠

我目前使用以下代码将所有请求传递给 index.php

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

index.php 文件处理所有请求。基于 ($_SERVER['REQUEST_URI']) index.php 从数据库加载内容。

我知道建议使用尾随斜杠,但希望不要使用;我认为 domain.com/sec/nom?x=23 看起来比 domain.com/sec/nom/?x=23 好


我尝试了一些自己的 .htaccess 变体,并在本地工作,但不是在 GoDaddy 上。

【问题讨论】:

    标签: php .htaccess redirect mod-rewrite


    【解决方案1】:

    像这样:

    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} !^www\.primary\.com$ [NC,OR]
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://www.primary.com%{REQUEST_URI} [L,NE,R=302]
    
    RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
    RewriteRule ^ /%1 [R=302,L]
    
    ## Unless directory, remove trailing slash
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+?)/$ /$1 [NE,R=302,L]
    
    RewriteRule ^index\.php$ - [L,NC]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php [L]
    

    【讨论】:

    • 谢谢,除了斜杠之外的所有工作,但通过添加:RedirectMatch 301 ^(.+)/$ $1
    • 是的,完美运行。谢谢
    猜你喜欢
    • 2017-05-03
    • 2014-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    • 2019-08-12
    • 2022-10-17
    相关资源
    最近更新 更多