我正在從事一個由軟體工程師和一個知道如何使用 Wordpress 的非技術人員團隊制作的專案。這意味著平臺的一部分是手工制作的,但仍然需要有非軟體工程師處理的 Wordpress 內容。我的想法是在我的網路服務器根目錄中有兩個檔案夾,一個名為 /app/ 包含手工代碼,一個名為 /wp/。因此,當 GitHub 管道將新代碼發布到 /app/ 時,肯定不會觸及 /wp/ 中包含 Wordpress 的內容。我已經實作了使用以下代碼強制使用 HTTPS:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
它就像一個魅力,如果用戶使用 HTTP,他們將被成功重定向。現在的問題是,我想為自己“保留”手工平臺使用的路線,如果用戶沒有呼叫這些路線,那么我將球傳給 Wordpress。我希望它顯示為單個網站,因此我不希望用戶加載 /app/appRoute 或 /wp/wpRoute,我希望始終加載 /route1、/route2,而不指定子檔案夾到 URL。手工平臺應該有優先權,它使用大約 13 條主要路線(其中一些有子路線),所以我猜我可以將它們硬編碼到 .htaccess 檔案中。如果用戶正在嘗試加載這些路由中的任何一個,那么我想將內容加載到 /app/route,如果沒有,我將加載 /wp/route。當然,Wordpress 有自己的 .htaccess 檔案,
RewriteEngine on
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
我非常不擅長處理 .htaccess 檔案并且一直在四處搜索,看起來我找不到適合我的場景的解決方案。我應該在網路服務器根目錄中有一個 .htaccess 檔案來洗掉 Wordpress 嗎?我應該有兩個 .htaccess,先寫 13 個路由,然后最終重定向到 WP .htaccess(甚至可能嗎?)?我是否冒著讓用戶面臨“重定向過多”錯誤的風險?
這種混合解決方案讓我很困惑。有同樣情況的人有建議嗎?先感謝您。
uj5u.com熱心網友回復:
鑒于以下要求:
/app子目錄包含“手工代碼”/wp子目錄包含 WordPress 站點。- 既不也不
/app應該/wp出現在可見的 URL 中。
.htaccess我應該在網路服務器根目錄中有一個檔案洗掉 Wordpress 嗎?
你可以,但我不會。將 WordPress.htaccess檔案保存在/wp子目錄中。WordPress 的所有內容都在/wp子目錄中。
我會使用 3 個.htaccess檔案:
檔案根目錄中的一個。
/app這管理到或/wp(WordPress)中的“手工代碼”的路由。這也應該管理規范的重定向(即 HTTP 到 HTTPS和www 與非 www)/app管理“手工代碼”中路由的子目錄中的一個。/wp在 WordPress 中管理路由的子目錄中的一個。
這使您可以將“手工代碼”和 WordPress 完全分開(在開發方面)。
您的 3 個.htaccess檔案將如下所示:
/.htaccess
# /.htaccess
RewriteEngine On
# HTTP to HTTPS redirect
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Rewrite specific URLs to "/app" (handmade code)
RewriteRule ^app-route-1$ app/$0 [L]
RewriteRule ^app-route-2$ app/$0 [L]
etc.
# Rewrite everything else to WordPress
RewriteRule (.*) wp/$1 [L]
/app如果有模式,可以組合“特定的重寫”。有關靜態資產,請參見下文。
/app/.htaccess
# /app/.htaccess
RewriteEngine On
# Redirect any direct requests to "/app" back to the root
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*) /$1 [R=301,L]
# Front-controller
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
/wp/.htaccess
# /wp/.htaccess
# Redirect any direct requests to "/wp" back to the root
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*) /$1 [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
# END WordPress
Note the RewriteBase directives and slash prefix on the substitution strings are specifically excluded, to avoid having to specify /app or /wp in the .htaccess file itself. (Although this might mess with WordPress, that likes to (unnecessarily) use RewriteBase and will try to overwrite the WP code block.)
You do not need to repeat the RewriteEngine directive, that already occurs later in the WP code block.
I don't know how you want to handle your static assets/resources (CSS, JS, images, etc.)? Currently, the above assumes that you will link directly to the assets within /app, ie. By including the /app path segment in the asset link. eg. <image src="/app/assets/images/myimage.png">. With WordPress you could link directly (ie. include /wp prefix) or omit /wp, since everything else is rewritten to /wp anyway.
Ideally, it would probably be preferable to omit both /app and /wp from your asset links, since you don't want to unnecessarily expose these to your users and it would otherwise make the sites dependent on these parent directories.
If your "handmade code" uses /assets for all the assets then you can rewrite these in the parent .htaccess file in the root, before your custom route rewrites:
# Rewrite "/app" assets
RewriteRule ^(assets)(?:/(.*)|$) app/$1/$2 [L]
This allows your "handmade code" to refer to assets using root-relative URLs, as if the app was installed in the document root.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/441232.html
標籤:WordPress 阿帕奇 .htaccess 改写 url重写
