.htaccess Riscrivi la sottodirectory di Wordpress senza rompere le altre sottodirectory

Aug 20 2020

Quindi ho WordPress installato /wp/ma voglio che tutti gli URL di WordPress vengano riscritti per apparire nella directory principale. Al momento ho questo lavoro (vedi sotto) ma voglio cambiare il mio in .htaccessmodo che non rompa altre sottodirectory.

Ad esempio, se voglio visitare www.example.com/images/image.png, attualmente mi reindirizza a una pagina 404 su WordPress.

È anche possibile con .htaccess?

Codice attuale:

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]

Aggiornamento : testato il suggerimento di MrWhite con la modalità di navigazione in incognito e un secondo browser. Nessun effetto. Nuovo .htaccessfile:

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

Risposte

MrWhite Aug 21 2020 at 01:54

È necessario aggiungere una condizione per escludere i file esistenti, altrimenti una richiesta di /images/image.pngverrà naturalmente riscritta /wp/images/image.pnge attiverà un 404 in WordPress (a meno che /wp/images/image.pngnon esistesse effettivamente).

Ad esempio, aggiungi la seguente condizione alla regola esistente:

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

AGGIORNAMENTO: questo non sembra funzionare. Va ancora alla pagina di WordPress 404

Hai messo la RewriteConddirettiva nel posto sbagliato.

RewriteCondle direttive non sono dichiarazioni del tutto separate, sono condizioni che si applicano alla RewriteRuledirettiva che segue. Di per sé, le RewriteConddirettive non fanno nulla.

La RewriteConddirettiva deve andare (ovunque) prima della RewriteRuledirettiva:

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]

Inoltre, dai un'occhiata al .htaccessfile "WordPress" nella /wpsottodirectory come riferimento. Questo fa lo stesso genere di cose.

A parte: a meno che tu non abbia più domini o sottodomini che non vuoi riscrivere, puoi rimuovere il controllo del nome host (cioè la RewriteConddirettiva che controlla la HTTP_HOSTvariabile del server).