.htaccess RewiteCond / RewriteRule Регулярное выражение Как захватить строковый шаблон, который не находится в первой позиции? - PullRequest
0 голосов
/ 17 ноября 2018

Когда я использую следующий URL, http://my.site.com/demo (с URL моего реального сайта) я получаю обратно: http://my.site.com/app_dev.php/controller/home

Вот наборы RewriteCond / RewriteRule, которые я использую в своемФайл .htaccess:

# Remove trailing / from all site URLs.  These only 'fire' when there is a trailing /.
RewriteCond %{HTTP_HOST} \.dev [NC]
RewriteCond %{REQUEST_URI} /$
RewriteRule ^(.+)/$ http://my\.dev\.site\.com/$1 [L]

# No need to exclude .dev, if present in the URL then it is handled above!
RewriteCond %{REQUEST_URI} /$
RewriteRule ^(.+)/$ http://my\.site\.com/$1 [L]

# The following redirects matches to the home page when the domain is specified
# with/without http{s} and/or www and there aren't any subdirectories.

# This redirect matches domain with .dev.
RewriteCond %{REQUEST_URI} ^$
RewriteCond %{HTTP_HOST} ^(?:http(?:s)?://)?(?:www\.)?my\.dev\.site\.com$ [NC]
RewriteRule .* http://my\.dev\.site\.com/app_dev\.php/controller/home/1a [L]

# This redirect matches domains without .dev.
RewriteCond %{REQUEST_URI} ^$
RewriteCond %{HTTP_HOST} ^(?:http(?:s)?://)?(?:www\.)?my\.site\.com$ [NC]
RewriteRule .* http://my\.site\.com/app.php/controller/home/1b [L]

# The following redirects matches to the home page when the subdirectories are /home or
# /app_dev/controller.

# This redirect matches domains with .dev and the subdirectories are /demo, and /home
# or /app_dev/controller.
RewriteCond %{HTTP_HOST} \.dev [NC]
RewriteCond %{REQUEST_URI} ^/demo$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/demo/home$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/app_dev\.php/controller/demo$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/app_dev\.php/controller/demo/home$ [NC]
RewriteRule .* http://my/.site/.com/app_dev\.php/controller/home/demo [L]

# *** This redirect matches domains without .dev and the subdirectories are /demo, and
# /home or /app/controller.
# No need to exclude .dev, if present in the URL then it is handled above!
RewriteCond %{REQUEST_URI} ^/demo$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/demo/home$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/app\.php/controller/demo$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/app\.php/controller/demo/home$ [NC]
RewriteRule .* http://my/.site/.com/app\.php/controller/home/demo [L]

# This redirect matches domains with .dev and the subdirectories are /call-sign and /home
# or /app_dev/controller.
RewriteCond %{HTTP_HOST} \.dev [NC]
RewriteCond %{REQUEST_URI} ^/[a-z0-9]+$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/[a-z0-9]+/home$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/app_dev.php/controller/[a-z0-9]+$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/app_dev.php/controller/[a-z0-9]+/home$ [NC]
RewriteRule ^(/[a-z0-9]+)?(?:/home)?(/[a-z0-9]+)?$ 
            http://my/.site/.com/app_dev\.php/controller/home$1$2 [L]

# This redirect matches domains without .dev and the subdirectories are /call-sign and
# /home or /app_dev/controller.
# No need to exclude .dev, if present in the URL then it is handled above!
RewriteCond %{REQUEST_URI} ^/[a-z0-9]+$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/[a-z0-9]+/home$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/app_dev.php/controller/[a-z0-9]+$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/app_dev.php/controller/[a-z0-9]+/home$ [NC]
RewriteRule ^(/[a-z0-9]+)?(/home)?(/[a-z0-9]+)?$ 
            http://my/.site/.com/app\.php/controller$1$2$3 [L]

# not sure what these do
RewriteCond %{REQUEST_URI} /Resources/public/jQueryFileUpload
RewriteCond %{REQUEST_URI} /Resources/public/
RewriteRule .? - [L]

RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]

RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]

# Handle all other Rewrite queries to the front controller here.
RewriteRule .? %{ENV:BASE}/app.php [L]

Я думаю, что набор RewriteCond / RewriteRule для демонстрационного случая /, отмеченный *** выше, правильно изменяет URL-адрес с http://my.site.com/app_dev.php/demo на http://my.site.com/app_dev.php/controller/home/demo,, но другой набор RewriteCond / RewriteRule вызывает удаление / demo из конца модифицированного URL, создавая http://my.site.com/app_dev.php/controller/home во время цикла RewriteCond / RewriteRule, который происходит при изменении URL.

Как я могу отследить это?Я видел ссылки на журнал трассировки, но я не знаю, как включить или просмотреть его.

Как можно предотвратить цикл RewriteCond / RewriteRule, который приводит к тому, что правильно измененный URL-адрес впоследствии изменяется итак теряет / демо в конце URL?

...