【问题标题】:htaccess reverting to php url when taking away www. or httpshtaccess 在带走 www 时恢复为 php url。或 https
【发布时间】:2021-12-26 20:51:16
【问题描述】:

我的 htaccess 文件重写规则有问题。我正在强制使用 HTTPS、WWW。和一个斜杠。如果我删除 WWW,重定向工作正常。或 HTTPS,然后它会与它们一起重定向。不错。

但是,如果我在 url 上附加了“/home/”之类的地方,我会删除 HTTPS 或 WWW。然后它会像“//index.php/?module=home”一样结束,而不是附加的“/home/”。

值得注意的是,它具有添加斜杠的正确行为。这不会搞砸。

完整规则:

RewriteRule ^image_prox/([%\w\.-_]*)$ /includes/image_proxy.php?url=$1 [N,NC]

RewriteRule ^/?wiki(/.*)?$ %{DOCUMENT_ROOT}/w/index.php [L]

RewriteRule ^mailinglist/$ /index.php?module=mailing_list [N]

RewriteRule ^articles/$ /index.php?module=home [N]

RewriteRule ^all-articles/$ /index.php?module=home&displayall [N,QSA]

RewriteRule ^all-articles/page\=([0-9]+)/$ /index.php?module=home&page=$1&displayall [N]

RewriteRule ^admin/$ /index.php?module=home [L]

RewriteRule ^latest-comments/$ /index.php?module=comments_latest [N]

RewriteRule ^latest-comments/page=([0-9]+)/$ /index.php?module=comments_latest&page=$1 [N,NC,QSA]

RewriteRule ^home/banned/$ /index.php?module=home&message=banned [N]

RewriteRule ^irc/$ /index.php?module=irc [N]

RewriteRule ^register/$ /index.php?module=register [N]

RewriteRule ^about-us/$ /index.php?module=about_us [N]

RewriteRule ^private-messages/$ /index.php?module=messages [N]

RewriteRule ^submit-article/$ /index.php?module=submit_article&view=Submit [N]

RewriteRule ^submit-article/error\=(.+?)?$ /index.php?module=submit_article&view=Submit&error=$1 [N,NC,QSA]

RewriteRule ^contact-us/$ /index.php?module=contact [N]

RewriteRule ^email-us/$ /index.php?module=email_us [N]

RewriteRule ^support-us/$ /index.php?module=support_us [N]

RewriteRule ^articles/$ /index.php?module=home [N]

RewriteRule ^articles/(.+?)\.([0-9]+)/$ /index.php?module=articles_full&title=$1&aid=$2 [N,NC,QSA]

RewriteRule ^articles/([0-9]+)/$ /index.php?module=articles_full&aid=$1 [N,NC,QSA]

RewriteRule ^articles/(.+?)\.([0-9]+)/error=(.+?)/?$ /index.php?module=articles_full&aid=$2&title=$1&error=$3 [N,NC,QSA]

RewriteRule ^articles/(.+?)\.([0-9]+)/page\=([0-9]+)/?$ /index.php?module=articles_full&aid=$2&title=$1&page=$3 [N,NC,QSA]

RewriteRule ^articles/(.+?)\.([0-9]+)/comment_id\=([0-9]+)/?$ /index.php?module=articles_full&aid=$2&title=$1&comment_id=$3 [N,NC,QSA]

RewriteRule ^articles/(.+?)\.([0-9]+)/article_page\=([0-9]+)/?$ /index.php?module=articles_full&aid=$2&title=$1&article_page=$3 [N,NC,QSA]

RewriteRule ^home/page\=([0-9]+)/$ /index.php?module=home&page=$1 [N]

RewriteRule ^private-messages/page\=([0-9]+)/$ /index.php?module=messages&page=$1 [N,NC,QSA]

RewriteRule ^private-messages/compose/(\d+)*/$ /index.php?module=messages&view=compose [N]

RewriteRule ^private-messages/compose/user\=([0-9]+)/$ /index.php?module=messages&view=compose&user=$1 [N,NC,QSA]

RewriteRule ^private-messages/([0-9]+)/$ /index.php?module=messages&view=message&id=$1 [N,NC,QSA]

RewriteRule ^private-messages/([0-9]+)/page\=([0-9]+)/$ /index.php?module=messages&view=message&id=$1&page=$2 [N,NC,QSA]

RewriteRule ^profiles/([0-9]+)/$ /index.php?module=profile&user_id=$1 [N,NC,QSA]

RewriteRule ^profiles/([0-9]+)/comments/$ /index.php?module=profile&view=more-comments&user_id=$1 [N,NC,QSA]

RewriteRule ^profiles/([0-9]+)/comments/page\=([0-9]+)$ /index.php?module=profile&view=more-comments&user_id=$1&page=$2 [N,NC,QSA]

RewriteRule ^users/statistics/$ /index.php?module=statistics [N,NC,QSA]

RewriteRule ^users/statistics/statid=([0-9]+)/$ /index.php?module=statistics&statid=$1&act=pick [N,NC,QSA]

RewriteRule ^articles/category/(.+?)/page\=([0-9]+)/$ /index.php?module=articles&view=cat&catid=$1&page=$2 [N,NC,QSA,B]

RewriteRule ^articles/category/(.+?)/$ /index.php?module=articles&view=cat&catid=$1 [N,NC,QSA,B]

RewriteRule ^itemdb/([0-9]+)/$ /index.php?module=items_database&view=item&id=$1 [N,NC,QSA]

RewriteRule ^itemdb/developer/([0-9]+)/$ /index.php?module=items_database&view=developer&id=$1 [N,NC,QSA]

RewriteRule ^itemdb/anticheats/([0-9]+)/$ /index.php?module=items_database&view=anticheats&id=$1 [N,NC,QSA]

RewriteRule ^steam-tracker/$ /index.php?module=steam_linux_share [N,NC,QSA]

RewriteRule ^steamplay/$ steamplay.php [N,NC,QSA]

RewriteRule ^steamplay/reports/([0-9]+)/$ steamplay_reports.php?id=$1 [N,NC,QSA]

RewriteRule ^steamplay/reports/([0-9]+)/page=([0-9]+)/$ steamplay_reports.php?id=$1&page=$2 [N,NC,QSA]

RewriteRule ^db/$ itemdb.php [N,NC,QSA]

RewriteRule ^free-games/$ free_games.php [N]

RewriteRule ^itemdb/steamid/([0-9]+)/$ itemdb.php?steamid=$1 [N,NC,QSA]

RewriteRule ^sales/page=([0-9]+)/$ /sales.php?page=$1 [N,NC,QSA]

RewriteRule ^sales/([0-9]+)/$ /sales.php?sale_id=$1 [N,NC,QSA]

RewriteRule ^sales/message=(.+?)/?$ /sales.php?message=$1 [N,NC,QSA]

RewriteRule ^sales/$ sales.php [N]

  Redirect 301 /podcast_rss_ogg.php /podcast_rss.php?format=ogg

  # make sure www. is always there
  RewriteCond %{HTTP_HOST} !^www\.
  RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

  RewriteCond %{HTTPS} off
  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

  # force trailing slash
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)([^/])$        /$1$2/ [L,R=301]
  
  # if requested url does not exist pass it as path info to index.php
  RewriteRule ^$ index.php?/ [QSA,L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule (.*) index.php?/$1 [QSA,L]

【问题讨论】:

    标签: apache .htaccess mod-rewrite


    【解决方案1】:

    您的外部重定向(非 www 到 www 和 HTTP 到 HTTPS)位于错误的位置。这些需要在文件顶部的现有重写之前进行。

    您需要清除浏览器缓存,因为错误的 301(永久)重定向已被浏览器缓存。首先使用 302(临时)重定向进行测试以避免缓存问题。

    (并且您需要将那些 N 标志更改为 L。)

    【讨论】:

    • 谢谢,我会移动它。所以尾随斜杠、HTTPS 和 WWW。应该高于其他规则?他们是否让 L 居于首位?
    • @NaughtySquid 是的,这 3 个 R=301 重定向需要移到顶部。是的,他们必须保留L 标志。 旁白: 触发重定向到/index.php/?module=home 的重写不在您发布的指令中,但包含在您的earlier question 中。但是请注意,此重定向将被缓存。
    • 再次感谢,现在一切似乎都运行良好。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2014-12-16
    • 2016-06-05
    • 2014-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-24
    • 2012-02-06
    相关资源
    最近更新 更多