![Apache - 重定向而不更改使用者瀏覽器中的 URL](https://rvso.com/image/760986/Apache%20-%20%E9%87%8D%E5%AE%9A%E5%90%91%E8%80%8C%E4%B8%8D%E6%9B%B4%E6%94%B9%E4%BD%BF%E7%94%A8%E8%80%85%E7%80%8F%E8%A6%BD%E5%99%A8%E4%B8%AD%E7%9A%84%20URL.png)
有沒有辦法使用 mod_rewrite 進行重定向而不更改使用者瀏覽器中的 URL?
我[P]
在 的末尾看到了一個使用的解決方案RewriteRule
,但這對我不起作用。
我想要的是:
https://my-server.com/propostas/billy.joe
< ---> internally redirect to
https://my-server.com/subdir/propostas_usuarios/billy.joe
我擁有的:
<LocationMatch "/propostas/(?<username>[^/]+)">
RewriteEngine On
RewriteRule ^/([^/]+)(.*) /subdir/propostas_usuarios/%{env:MATCH_USERNAME}
</LocationMatch>
這就是目前正在進行的工作。但重定向後,我可以/subdir/propostas_usuarios
在新的 URL 中看到。
[P]
我嘗試過這樣使用:
RewriteRule (.*) https://%{SERVER_NAME}/hpe/propostas_usuarios/%{env:MATCH_USERNAME} [P]
但這給了我這個錯誤:
[Fri Dec 11 16:02:57.945091 2020] [proxy:debug] [pid 16725:tid 140351293593344] mod_proxy.c(1253): [client 10.0.105.36:52700] AH01143: Running scheme https handler (attempt 0)
[Fri Dec 11 16:02:57.945102 2020] [proxy_ajp:debug] [pid 16725:tid 140351293593344] mod_proxy_ajp.c(744): [client 10.0.105.36:52700] AH00894: declining URL https://my-server.com/subdir/propostas_usuarios/billy.joe
[Fri Dec 11 16:02:57.945124 2020] [proxy_fcgi:debug] [pid 16725:tid 140351293593344] mod_proxy_fcgi.c(1032): [client 10.0.105.36:52700] AH01076: url: https://my-server.com/subdir/propostas_usuarios/billy.joe proxyname: (null) proxyport: 0
[Fri Dec 11 16:02:57.945131 2020] [proxy_fcgi:debug] [pid 16725:tid 140351293593344] mod_proxy_fcgi.c(1035): [client 10.0.105.36:52700] AH01077: declining URL https://my-server.com/subdir/propostas_usuarios/billy.joe
[Fri Dec 11 16:02:57.945149 2020] [proxy:debug] [pid 16725:tid 140351293593344] proxy_util.c(2338): AH00942: HTTPS: has acquired connection for (*)
[Fri Dec 11 16:02:57.945159 2020] [proxy:debug] [pid 16725:tid 140351293593344] proxy_util.c(2393): [client 10.0.105.36:52700] AH00944: connecting https://my-server.com/subdir/propostas_usuarios/billy.joe to my-server.com:443
[Fri Dec 11 16:02:57.946130 2020] [proxy:debug] [pid 16725:tid 140351293593344] proxy_util.c(2616): [client 10.0.105.36:52700] AH00947: connected /subdir/propostas_usuarios/billy.joe to my-server.com:443
[Fri Dec 11 16:02:57.946210 2020] [proxy:debug] [pid 16725:tid 140351293593344] proxy_util.c(3085): AH02824: HTTPS: connection established with 10.30.6.52:443 (*)
[Fri Dec 11 16:02:57.946233 2020] [proxy:error] [pid 16725:tid 140351293593344] AH00961: HTTPS: failed to enable ssl support for 10.30.6.52:443 (my-server.com)
[Fri Dec 11 16:02:57.946236 2020] [proxy:debug] [pid 16725:tid 140351293593344] proxy_util.c(2353): AH00943: HTTPS: has released connection for (*)
有任何想法嗎?
答案1
該P
標誌透過 mod_proxy 發送請求 - 這裡似乎不需要。您只需要對另一個子目錄進行內部重寫。
<LocationMatch "/propostas/(?<username>[^/]+)"> RewriteEngine On RewriteRule ^/([^/]+)(.*) /subdir/propostas_usuarios/%{env:MATCH_USERNAME} </LocationMatch>
這看起來應該可行 - 正如您所觀察到的,這裡沒有外部重定向。但是,該指令上沒有L
(oe END
) 標誌RewriteRule
,因此處理將繼續,並且可能是稍後的指令觸發重定向? (雖然外部重定向確實應該是前任何內部重寫。
如果您還有一個.htaccess
檔案(或<Directory>
容器),那麼這些指令也會稍後處理,並且可能會觸發重定向。 (?)
該規則也可以“簡化” - 不需要包裝器,因為它可以在單一指令<LocationMatch>
中完成。RewriteRule
例如:
RewriteEngine On
RewriteRule ^/propostas/([^/]+)$ /subdir/propostas_usuarios/$1 [L]
我在正規表示式上放置了一個字串結尾錨點,否則它將匹配表單的 URL /propostas/billy.joe/anything
。這$1
是對第一個捕獲組的反向引用(即使用者名稱) 在裡面RewriteRule
圖案。
如上所述,我添加了該L
標誌以防止當前上下文中正在處理進一步的指令。但是,<Directory>
容器 ( 和)中的指令.htaccess
仍將被處理。
如果您之前看到外部重定向,那麼您需要在測試之前確保瀏覽器快取已清除。