.htaccess他のサブディレクトリを壊さずにWordpressサブディレクトリを書き換えます

Aug 20 2020

だから私はWordPressをインストールして/wp/いますが、すべてのWordPressURLを書き直してルートディレクトリに表示したいと思います。私は現在これを機能させています(以下を参照)が、.htaccess他のサブディレクトリを壊さないように変更したいと思います。

たとえば、にアクセスしたい場合www.example.com/images/image.png、現在、WordPressの404ページに再ルーティングされます。

これはでさえ可能.htaccessですか?

現在のコード:

RewriteEngine on

#Wordpress
# Rewrites all URLS without wp in them
RewriteCond %{REQUEST_URI} !^/wp/

# Rewrites all URLS [Replace "example" with the actual domain, without the TLD (.com, .net, .biz, etc)]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.

# Rewrite all those to insert /folder
RewriteRule ^(.*)$ /wp/$1 [L]

更新:シークレットモードと2番目のブラウザでMrWhiteの提案をテストしました。無効。新しい.htaccessファイル:

RewriteEngine on

#Wordpress
# Rewrites all URLS without wp in them
RewriteCond %{REQUEST_URI} !^/wp/

# Rewrites all URLS [Replace "example" with the actual domain, without the TLD (.com, .net, .biz, etc)]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.

# Rewrite all those to insert /folder
RewriteRule ^(.*)$ /wp/$1 [L]
# Request does not map to an existing file
RewriteCond %{REQUEST_FILENAME} !-f

回答

MrWhite Aug 21 2020 at 01:54

既存のファイルを除外する条件を追加する必要があります。そうしないと、リクエスト/images/image.pngが自然に書き換えられ/wp/images/image.png、WordPressで404がトリガーされます(/wp/images/image.png実際に存在しない場合)。

たとえば、既存のルールに次の条件を追加します。

# Request does not map to an existing file
RewriteCond %{REQUEST_FILENAME} !-f

更新:これは機能していないようです。それでもWordPress404ページに移動します

あなたは入れているRewriteCond間違った場所にディレクティブを。

RewriteCondディレクティブは完全に別個のステートメントではなく、後続のディレクティブに適用される条件ですRewriteRuleRewriteCondディレクティブ自体は何もしません。

RewriteCond(どこかに)行くためのディレクティブのニーズの前にRewriteRuleディレクティブ:

RewriteEngine on

#Wordpress
# Rewrites all URLS without wp in them
RewriteCond %{REQUEST_URI} !^/wp/

# Rewrites all URLS [Replace "example" with the actual domain, without the TLD (.com, .net, .biz, etc)]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.

# Request does not map to an existing file
RewriteCond %{REQUEST_FILENAME} !-f

# Rewrite all those to insert /folder
RewriteRule ^(.*)$ /wp/$1 [L]

また、サブディレクトリの「WordPress」.htaccessファイル/wpを参照してください。それは同じようなことをします。

余談ですが、書き換えたくないドメインまたはサブドメインが複数ある場合を除いて、ホスト名チェック(つまりRewriteCondHTTP_HOSTサーバー変数をチェックするディレクティブ)を削除できます。