.htaccess RewiteCond / RewriteRule正则表达式如何捕获不在第一个位置/ home的字符串模式?

时间:2018-11-16 23:32:53

标签: mod-rewrite

当我使用以下URL时,http://my.site.com/demo(带有我实际站点的URL)会返回:http://my.site.com/app_dev.php/controller/home

这是我在.htaccess文件中使用的RewriteCond / RewriteRule集:

# 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]

我认为/ demo情况的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的过程中在修改URL时发生RewriteCond / RewriteRule循环。

我如何追踪呢?我已经看到了对跟踪日志的引用,但是我不知道如何启用它或查看它。

如何防止RewriteCond / RewriteRule循环导致随后正确更改了正确的URL,从而使URL末尾的/ demo失去了?

0 个答案:

没有答案