2015-12-29 49 views
8

Lavoro con un framework chiamato SilverStripe ... al momento stiamo migrando un vecchio sito su questo framework. Il problema è che i vecchi URL del sito terminavano con .php o .html mentre nel nuovo sito non lo erano..htaccess redirect .php e .html richieste

Ho bisogno di modificare la seconda regola di riscrittura in modo tale da pompare la richiesta su main.php senza estensioni .html o .php.

Nel mio .htaccess corrente ho le seguenti regole:

# Turn off index.php handling requests to the homepage fixes issue in apache >=2.4 
<IfModule mod_dir.c> 
    DirectoryIndex disabled 
</IfModule> 

SetEnv HTTP_MOD_REWRITE On 
RewriteEngine On 

# Enable HTTP Basic authentication workaround for PHP running in CGI mode 
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] 

# Deny access to potentially sensitive files and folders 
RewriteRule ^vendor(/|$) - [F,L,NC] 
RewriteRule silverstripe-cache(/|$) - [F,L,NC] 
RewriteRule composer\.(json|lock) - [F,L,NC] 

# Process through SilverStripe if no file with the requested name exists. 
# Pass through the original path as a query parameter, and retain the existing parameters. 
RewriteCond %{REQUEST_URI} ^(.*)$ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule .* framework/main.php?url=%1 [QSA] 

# If framework isn't in a subdirectory, rewrite to installer 
RewriteCond %{REQUEST_URI} ^(.*)/framework/main.php$ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule . %1/install.php? [R,L] 

Possibile soluzione (ancora testare):

RewriteCond %{REQUEST_URI} ^(.*)\.html [OR] 
RewriteCond %{REQUEST_URI} ^(.*)\.php [OR] 
RewriteCond %{REQUEST_URI} ^(.*)$ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule .* framework/main.php?url=%1 [QSA] 

risposta

4

Aggiungere le seguenti regole al file .htaccess sotto la # Deny access to potentially sensitive files and folders blocco di regole:

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI} ^(.+)\.html$ 
RewriteRule (.*)\.html$ /$1 [R=301,L] 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI} ^(.+)\.php$ 
RewriteRule (.*)\.php$ /$1 [R=301,L] 

Le prime due righe controllare che l'URL non è una directory e non è il file.

La terza riga verifica che l'url contenga o .html o .php.

La linea indietro rimuove .html/.php dall'URL

+0

@ 3dogo sfortunatamente la regola non si attiva –

+0

Questo è strano. Funziona per me con i miei test. Ad esempio, se visito "www.example.com/about.html", verrà reindirizzato a "www.example.com/about". Un altro esempio 'www.esempio.com/about/team.php' verrà reindirizzato a' www.esempio.com/about/team'. – 3dgoo

+0

con quale versione di Apache stai testando? –

0

Come circa la base ..

Redirect 301 /about-us.html http://www.domain.com/about-us 

EDIT Ora hai menzionato che ne hai centinaia ... la risposta di cui sopra è ancora valida in quanto puoi aggiungerne centinaia all'htaccess (l'ho visto molte volte) ... tuttavia è molto possibile anche in questo modo ...

RedirectMatch 301 (.*)\.html$ http://www.domain.com$1/ 

L'aspetto negativo di non fare questa riga per riga è ora che si può avere un file html specifico che si vuole consentire l'accesso a fermo e che avrà bisogno di aggiungere come eccezione a questa regola.

+0

Perché ho circa 900 di tali URL :) –

+0

Risponde alla loro domanda, semplicemente accade che ora hanno fornito nuove informazioni! – Barry

+0

è in qualche modo possibile modificare la regola ... RewriteRule. * Framework/main.php? Url =% 1 [QSA] tale che ciò che viene passato in quel parametro url non ha mai un'estensione .html o .php? –

3

Ecco una breve soluzione al problema

provare la seguente regola:

RewriteRule ^([^.]+)\.(html|php)$ /framework/main.php?url=$1 [NC,L,QSA] 

Ciò riscrivere

/file.php OR /file.html 

a

/framework/main.php?url=$1 

modello ha spiegato:

^([^.] +) (Html ​​| php) $ corrisponde a qualsiasi richiesta di iniziare con qualsiasi carattere ad esclusione di punti seguiti da un char dot littrel seguito da php letterale. o html alla fine dell'uri.

Nota: questa dovrebbe essere la prima regola nel tuo htaccess prima di qualsiasi altra regola.

4

Si può solo modificare la regola esistente un po ':

# Turn off index.php handling requests to the homepage fixes issue in apache >=2.4 
<IfModule mod_dir.c> 
    DirectoryIndex disabled 
</IfModule> 

SetEnv HTTP_MOD_REWRITE On 
RewriteEngine On 

# Enable HTTP Basic authentication workaround for PHP running in CGI mode 
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] 

# Deny access to potentially sensitive files and folders 
RewriteRule ^vendor(/|$) - [F,L,NC] 
RewriteRule silverstripe-cache(/|$) - [F,L,NC] 
RewriteRule composer\.(json|lock) - [F,L,NC] 

# Process through SilverStripe if no file with the requested name exists. 
# Pass through the original path as a query parameter, and retain the existing parameters. 
# Strip out .html or .php from request URI 
RewriteCond %{REQUEST_URI} ^(.+?)(?:\.(?:html|php))?$ [NC] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule^framework/main.php?url=%1 [L,QSA] 

# If framework isn't in a subdirectory, rewrite to installer 
RewriteCond %{REQUEST_URI} ^(.*)/framework/main\.php$ [NC] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule . %1/install.php? [R,L] 
3

Prova di questo codice: -

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.html -f 
RewriteRule ^(.*)$ $1.html 

Spero che questo codice funziona per voi :)