¿Cómo puedo hacer que mis reglas de salto .htaccess sean condicionalmente?
-
10-07-2019 - |
Pregunta
Deseo redirigir todas las URL que no contengan " _js ", " _css " ;, " _img " ;, etc., a mi secuencia de comandos de envío. De alguna manera no funciona. Por ejemplo, no se puede acceder a todos los archivos dentro de mi carpeta / _js / (es decir: se envían a index.php en lugar de acceder a los archivos físicos que residen en esa carpeta).
Aquí está mi htaccess:
IndexIgnore *
Options +FollowSymLinks
RewriteEngine on
# if the following conditions are met, SKIP the rewriteRules.
RewriteCond %{REQUEST_URI} ^/(_admin/¦_css/¦_js/¦_img/)
RewriteRule . - [S=9]
# Externally redirect to add missing trailing slash
RewriteRule ^(([a-z0-9._\-]+/)*[a-z0-9_\-]+)$ http://%{HTTP_HOST}/$1/?%{QUERY_STRING} [NC,R,L]
# SIX PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4&id=$5&title=$6&%{QUERY_STRING} [NC,L]
# FIVE PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4&id=$5&%{QUERY_STRING} [NC,L]
# FOUR PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4&%{QUERY_STRING} [NC,L]
# THREE PARAMS : projects/touch/texts/
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&%{QUERY_STRING} [NC,L]
# TWO PARAMS: downloads
RewriteRule ^downloads/([^/]+)/$ index.php?section=downloads&item=$1&%{QUERY_STRING} [NC,L]
# TWO PARAMS:
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&%{QUERY_STRING} [NC,L]
# TAG URL : index.php?tag=space%2C+navigable+music#5
RewriteRule ^tag/([a-z0-9_\-]+)/$ index.php?tag=$1&%{QUERY_STRING} [NC,L]
# ONE PARAM
RewriteRule ^([a-z0-9_\-]+)/$ index.php?section=$1&%{QUERY_STRING} [NC,L]
Editar:
Tenga en cuenta la estructura de mi carpeta. ¿Podría ser la fuente del problema?
Tengo un " v1 " y "v2" estructura de carpetas Este .htaccess se encuentra en el " v2 " carpeta. un nivel arriba, tengo un .htaccess que redirige todas las solicitudes a " v2 " ;.
root
L.htaccess << dispatch between v1 and v2 folders
L v1 L v2 L.htaccess << the .htaccess code posted above
L _admin L all my website files & folders
Solución
Está utilizando el carácter incorrecto, & # 166;
( barra rota , U + 00A6) en lugar de |
( línea vertical , U + 007C) y patrón incorrecto para REQUEST_URI
.
RewriteCond %{REQUEST_URI} ^/v2/(_admin/|_css/|_js/|_img/)
RewriteRule . - [S=9]
O para el archivo .htaccess en su directorio v2 simplemente:
RewriteRule ^_(admin|css|js|img)/ - [S=9]
Otros consejos
Aquí hay algunas soluciones aleatorias que pueden o no solucionar el problema real, que no está totalmente claro (vea mi comentario) y que podrían ser de ayuda si controla el servidor y habilita RewriteLog, a través de:
RewriteLog "/tmp/rewrite.log"
RewriteLogLevel 9
pero debe poner esto en la configuración del servidor principal.
Dicho esto, dos problemas principales:
- Falta de uso de la bandera QSA (no realmente relevante)
- Uso excesivo de barras (probablemente relevante en el problema real, vea la nueva versión de la regla de omisión)
Aquí va el archivo modificado
IndexIgnore *
Options +FollowSymLinks
RewriteEngine on
# if the following conditions are met, SKIP the rewriteRules.
RewriteCond %{REQUEST_URI} ^(_admin¦_css¦_js¦_img)
RewriteRule . - [L]
# Externally redirect to add missing trailing slash. Not really needed, AFAICS
# RewriteRule ^(([^/]+/)*[^/]+)$ http://%{HTTP_HOST}/$1/ [NC,R,L,QSA]
# SIX PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4&id=$5&title=$6 [NC,L,QSA]
# FIVE PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4&id=$5 [NC,L,QSA]
# FOUR PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4 [NC,L,QSA]
# THREE PARAMS : projects/touch/texts/
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3 [NC,L,QSA]
# TWO PARAMS: downloads
RewriteRule ^downloads/([^/]+)/?$ index.php?section=downloads&item=$1 [NC,L,QSA]
# TWO PARAMS:
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2 [NC,L,QSA]
# TAG URL : index.php?tag=space%2C+navigable+music#5
RewriteRule ^tag/([^/]+)/?$ index.php?tag=$1 [NC,L,QSA]
# ONE PARAM
RewriteRule ^([^/]+)/?$ index.php?section=$1 [NC,L,QSA]
EDITAR: Dada la estructura de carpetas explicada, intente agregar al v2 .htaccess al principio lo siguiente:
RewriteBase /
Todavía no ha explicado si puede o no puede usar RewriteLog (supongo que no puede)