You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.htaccess实现无URL变更的HTTP转HTTPS短URL映射求助

Fixing the Short URL Rewrite with Address Bar Preservation (HTTP → HTTPS)

Hey there! Let's work through this short URL setup you need. Since you want http://example.com/shorturl to serve content from https://example.com/apps/appname without changing the browser's address bar, and you already have HTTPS redirects working for the target path, here's how to make this happen with .htaccess:

Step-by-Step .htaccess Rules

First, create or edit the .htaccess file in your website's root directory (where shorturl would live) and add these rules:

# Turn on the rewrite engine
RewriteEngine On

# 1. Redirect HTTP version of shorturl to HTTPS (matches your existing IIS HTTPS policy)
RewriteCond %{HTTPS} off
RewriteRule ^shorturl$ https://%{HTTP_HOST}/shorturl [R=301,L]

# 2. Internally proxy the HTTPS shorturl to your target app path (keeps address bar unchanged)
RewriteRule ^shorturl$ https://example.com/apps/appname [P,L]

What Each Part Does:

  • RewriteEngine On: Enables Apache's URL rewriting functionality (required for all these rules).
  • The first rule checks if the connection is not HTTPS (%{HTTPS} off) and redirects the HTTP shorturl to its HTTPS equivalent. The [R=301,L] flags mean:
    • R=301: Permanent redirect (good for SEO and browser caching)
    • L: Stop processing further rules once this one runs
  • The second rule uses the [P] (Proxy) flag to tell Apache to fetch content from https://example.com/apps/appname behind the scenes while keeping https://example.com/shorturl in the browser's address bar. The L flag ensures no other rules run after this.

Important Notes for Newbies

  • Mod_Proxy Requirement: The [P] flag relies on Apache's mod_proxy and mod_proxy_http modules being enabled. If you're on shared hosting, you might need to check with your host to confirm these are turned on.
  • SSL Certificate Issues: If your server throws errors when proxying to the HTTPS target, you may need to add these lines above the rewrite rules to disable strict SSL checks (safe if the target is on the same domain):
    SSLProxyEngine On
    SSLProxyVerify none
    SSLProxyCheckPeerCN off
    SSLProxyCheckPeerName off
    
  • Test First: Always test with a R=302 (temporary redirect) first before switching to R=301, so you don't accidentally cache a broken redirect in your browser.

内容的提问来源于stack exchange,提问作者newb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:04:48