.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 HTTPshorturlto 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 fromhttps://example.com/apps/appnamebehind the scenes while keepinghttps://example.com/shorturlin the browser's address bar. TheLflag ensures no other rules run after this.
Important Notes for Newbies
- Mod_Proxy Requirement: The
[P]flag relies on Apache'smod_proxyandmod_proxy_httpmodules 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 toR=301, so you don't accidentally cache a broken redirect in your browser.
内容的提问来源于stack exchange,提问作者newb
相关产品推荐
相关产品推荐

